[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Perl one line fix

Posted on 2014-01-03
5
Medium Priority
?
266 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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

649 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