Solved

Perl one line fix

Posted on 2014-01-03
5
258 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
Comment Utility
my @ds = split /(?<=data5),/;
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
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
Comment Utility
Hi Ozo ,

Worked as per my needs. Great thanks.

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

Thanks
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
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
Comment Utility
Ozo , Many thanks for the explanation.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

772 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now