Solved

PERL format

Posted on 2009-07-06
3
143 Views
Last Modified: 2012-05-07
I have string $str = "20090630|A|0.5
                                 20090630|B|0.7";
basically a file wiht 2 lines is read in to a string.
I want to output string in the format 20090630,0.5,0.7
Can this be done in one liner ?
I know we can split with "\n" first and then with "|" but i want to know if it can be done in one line ?

Thanks for your help
0
Comment
Question by:koppcha
3 Comments
 
LVL 40

Assisted Solution

by:mrjoltcola
mrjoltcola earned 50 total points
ID: 24791366
my $str = "20090630|A|0.5
        20090630|B|0.7";

$str =~ /([^|]+)\|([^|]+)\|([^|\s]+)\s+([^|]+)\|([^|]+)\|([^|]+)/m;
print "$1,$3,$6\n";
0
 
LVL 39

Accepted Solution

by:
Adam314 earned 200 total points
ID: 24791376

my $str = "20090630|A|0.5
           20090630|B|0.7";
 
print "$1,$2,$3\n" if $str =~ /(\d+)\|\w+\|([\d\.]+).*\|([\d\.]+)$/s;

Open in new window

0
 
LVL 84

Expert Comment

by:ozo
ID: 24791668
my $str = "20090630|A|0.5
        20090630|B|0.7";
print join",",split/(?:\s+\d+)?\|\w+\|/,"$str\n";
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…

713 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