?
Solved

Perl one line fix

Posted on 2014-01-03
5
Medium Priority
?
264 Views
Last Modified: 2014-01-03
Hi,

This is my input:

data1,data2,data5,data6,data3,data4
data1,data2,data5,data7,data9,data8

I want output as below ( spliting each line by 3 columns )

data1,data2,data5
data6,data3,data4
data1,data2,data5
data7,data9,data8

This the code i have tried:
But i am getting output excluding data5 , please advice

#!/usr/bin/perl
use strict;
use warnings;
my $file = shift or die "usage: $0 Inputfile";
open FH , $file or die "unable to open $file , $!";
my @data = map { chomp; $_ } <FH>;
#print join ("\n",@data);
foreach (@data) {
my @ds = split /data5,/, $_;
print $_ , "\n" for @ds;
}

Open in new window


Thanks,
Magento
0
Comment
Question by:magento
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 2000 total points
ID: 39755564
my @ds = split /(?<=data5),/;
0
 
LVL 84

Expert Comment

by:ozo
ID: 39755578
or
perl -pe 's/(?<=data5),/\n/' input
or
perl -F, -ane '$"=",";print "@F[0..2]\n@F[3..5]"' input
or
perl -pe '$"=s/(,.*?,.*?),/$1\n/' input
0
 
LVL 5

Author Comment

by:magento
ID: 39755606
Hi Ozo ,

Worked as per my needs. Great thanks.

Can you advice what actually (?<=data5), does?

Thanks
0
 
LVL 84

Expert Comment

by:ozo
ID: 39755610
perl -MYAPE::Regex::Explain -e 'print YAPE::Regex::Explain->new(qr/(?<=data5),/)->explain'
The regular expression:

(?-imsx:(?<=data5),)

matches as follows:
 
NODE                     EXPLANATION
----------------------------------------------------------------------
(?-imsx:                 group, but do not capture (case-sensitive)
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  (?<=                     look behind to see if there is:
----------------------------------------------------------------------
    data5                    'data5'
----------------------------------------------------------------------
  )                        end of look-behind
----------------------------------------------------------------------
  ,                        ','
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------
0
 
LVL 5

Author Closing Comment

by:magento
ID: 39755651
Ozo , Many thanks for the explanation.
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question