Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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.


In reply to Re: Perl Extract specific string by Anonymous Monk
in thread Perl Extract specific string by MacLing

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2024-04-19 19:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found