http://www.perlmonks.org?node_id=1017936


in reply to Perl Extract specific string

I agree, with great minds that has given there wisdom on this. However, since is your first time in the house, I will cut you some slack on the rule of the house which you can read for yourself here How do I post a question effectively?.

I can think of three ways of getting to your desired output:

However, pay attention this "..Can you show some more lines of your data? Especially when you have to write perhaps a regular expression to extract the data it is always good to see some more data, to check how "regular" your data structure really is..." by CountZero and others before.
Hope this help in some ways.

Replies are listed 'Best First'.
Re^2: Perl Extract specific string
by MacLing (Novice) on May 19, 2014 at 01:54 UTC

    Thanks CountZero !!!

    please help me again.

    #!/usr/bin/perl use warnings; use strict; my $str = <<'_STR_'; 09.05.2014 09.49.52:359 RID:routerNode1@app1:1662306081,msisdn:7878872 +25696,sid:93889095007001,tid:1405090902095648846024000,status:2,time: +20140509094952,reason:DELIVRD,refund:null,status2:null _STR_ my $wanted_data; for ( split /,/, $str ) { if (/(\d{2}\.\d{2}\.\d{4}' '\d{2}\.\d{2}\.\d{2}).+RID:(\d+)/sm) { my ( $format_date, $rid )= ( $1, $2 ); $format_date =~ s/(\d{4})(\d{2})(\d{2})/$1-$2-$3/; $wanted_data .= $format_date . ',' . $rid; } elsif (/msisdn:(.+)/) { $wanted_data .= ',' . $1; } elsif (/sid:(.+)/) { $wanted_data .= ',' . $1; } elsif (/tid:(.+)/) { $wanted_data .= ',' . $1; } elsif (/status:(.+)/) { $wanted_data .= ',' . $1; } elsif (/time:(.+)/) { $wanted_data .= ',' . $1; } else{ print "data given is not true"} } print $wanted_data, $/;

    the wanted_data like this below :

    09-05-2014,09-49-52,routerNode1@app1:1662306081,787887225696,93889095007001,1405090902095648846024000,2,20140509094952

    when run, error like below:

    data given is not truedata given is not truedata given is not truedata given is not true,787887225696,93889095007001,1405090902095648846024000,2,20140509094952

    and please advice, how to read the input file and write to the output file.

    Thank you.