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}->($_); } }
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 |
In Section
Seekers of Perl Wisdom