Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Perl Extract specific string

by Anonymous Monk
on Feb 09, 2013 at 09:02 UTC ( [id://1017936]=note: print w/replies, xml ) Need Help??


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:

  • see Text::CSV or Test::CSV_XS. Since you presented a Comma seperated string
  • use split,conditional loop "if" and regex like this:
    #!/usr/bin/perl use warnings; use strict; my $str = <<'_STR_'; 06.02.2013 12.24.01:909 5807225321 INFO {EXT:httpadapter:17:14:}[0]RUL +EZ HTTPADAPTER,msisdn:637584930382,ud:Pan,trxtime:20130206122401 Resp +onseDeltaTime:31 ms ResponseCode:200 ResponseBody:OK _STR_ my $wanted_data; for ( split /,/, $str ) { if (/(\d{2}\.\d{2}\.\d{4})/) { my $format_date = $1; $format_date =~ s/\./-/g; $wanted_data .= $format_date; } elsif (/msisdn:(.+)/) { $wanted_data .= ',' . $1; } elsif (/ud:(.+)/) { $wanted_data .= ',' . $1; } elsif (/trxtime:(\d{8}).+Code:(\d+)/sm) { my ( $format_date, $code ) = ( $1, $2 ); $format_date =~ s/(\d{4})(\d{2})(\d{2})/$1-$2-$3/; $wanted_data .= ',' . $format_date . ',' . $code; }else{ print "data given is not true"} } print $wanted_data, $/;
  • Or you can use "dispatch table" instead of the ifs and elsifs like this:
    #!/usr/bin/perl use 5.014; ## it wouldn't work for lower version use Readonly; Readonly my $comma => ","; my $str = <<'_STR_'; 06.02.2013 12.24.01:909 5807225321 INFO {EXT:httpadapter:17:14:}[0]RUL +EZ HTTPADAPTER,msisdn:637584930382,ud:Pan,trxtime:20130206122401 Resp +onseDeltaTime:31 ms ResponseCode:200 ResponseBody:OK _STR_ my %data_filter = ( '1_first_date_format' => sub { if ( $_[0] =~ /(\d{2}\.\d{2}\.\d{4})/ ) { my $format_date = $1; $format_date =~ s/\./-/g; return ( $format_date, $comma ); } }, '2_msisdn' => sub { if ( $_[0] =~ /msisdn:(.+)/ ) { return ( $1, $comma ); } }, '3_ud' => sub { if ( $_[0] =~ /ud:(.+)/ ) { return ( $1, $comma ); } }, '4_trxtime' => sub { if ( $_[0] =~ /trxtime:(\d{8}).+Code:(\d+)/sm ) { my ( $format_date, $code ) = ( $1, $2 ); $format_date =~ s/(\d{4})(\d{2})(\d{2})/$1-$2-$3/; return ( $format_date, $comma, $code ); } }, ); for my $key ( sort keys %data_filter ) { for ( split /,/, $str ) { print $data_filter{$key}->($_); } }
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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1017936]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-19 03:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found