Solved

Perl one line fix

Posted on 2014-01-03
5
260 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
  • 3
  • 2
5 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 500 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

785 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