Need to extract words from a file before and after ^A in perl -
i have many log files cancel_log1,cancel_log2...
all files contains logs this
2013/05/08 17:09:18 -0700 766 | 1368058158 | 22991 | yapache | cancelfeedback | info | file: /home/y/share/uni/sites/order/cache/views/root:order:bucket=a:cancelservice:cache function: () line: 450 online^atoo_expensive^amoney tight. keep service don't have money @ time. maybe can come in future.^asecuresanctuary.org^aweb hosting^asecuresanctuary^a05/09/2009^a05/08/2013 2013/05/07 17:45:35 -0700 219 | 1367973935 | 23388 | yapache | cancelfeedback | info | file: /home/y/share/uni/sites/order/cache/views/root:order:bucket=a:cancelservice:cache function: () line: 450 online^aother^ayahoo china service close^alifesig.com^aweb hosting^akennethli2005^a05/10/2008^a05/07/2013 2013/05/08 17:30:57 -0700 115 | 1368059457 | 22982 | yapache | cancelfeedback | info | file: /home/y/share/uni/sites/order/cache/views/root:order:bucket=a:cancelservice:cache function: () line: 450 online^atoo_expensive^a^asecuresanctuary.org^aweb hosting^asecuresanctuary^a05/09/2009^a05/08/2013 2013/05/07 17:59:38 -0700 694 | 1367974778 | 23381 | yapache | cancelfeedback | info | file: /home/y/share/uni/sites/order/cache/views/root:order:cancelstep5:cache function: () line: 436 online^amissing_feature^a^achuanqisf244baidu.com^adomains^achuanqisf244baidu^a05/07/2013^a05/07/2013 2013/05/08 17:33:03 -0700 815 | 1368059583 | 23000 | yapache | cancelfeedback | info | file: /home/y/share/uni/sites/order/cache/views/root:order:cancelstep5:cache function: () line: 436 online^aretired^a^asisterthrifty.com^adomains^atrinaboice^a08/09/2005^a05/08/2013 2013/05/07 17:59:40 -0700 231 | 1367974780 | 23389 | yapache | cancelfeedback | info | file: /home/y/share/uni/sites/order/cache/views/root:order:cancelstep5:cache function: () line: 436 online^amissing_feature^a^achuanqisf239baidu.com^adomains^achuanqisf239baidu^a05/07/2013^a05/07/2013 i want extract words separated ^a , write csv file.
for example output file this:
missing_feature chuanqisf239baidu.com domains chuanqisf239baidu any appreciated.
you can easily split on ^a field , filter out data afterwards. took range of columns indicated interested in , added quote logic before joining them commas.
while ( <> ) { join( ',', map { index( $_, ',' ) > -1 ? qq/"$_"/ : $_ } @{[ split /\^a/ ]}[1..5] ); } to break down few more steps goes this.
i use "diamond operator" because shouldn't need me write file handling code if extracting data main problem. use generic input loop.
so
splitline so:split /\^a/, gives list.we take slice of that list performing operation within slice expression. if have array
@a,@a[2..4]way extract elements you're interested in.@{[ split /\^a/ ]}"array expression", ,@{[ split /\^a/ ]}[1..5]slice of array.but it's list other, putting in
mapexpression, check see whether has comma in field, if wrap in double quotes (qq/"$_"/) if not return itself.then use
joininsert comma between each of fields, ,sayresulting string.
however, approach above poor way csv, half way. in real csv output have take care of possible embedded quotes, should quote field.
so text::csv, that's:
use text::csv; $csv = text::csv->new ( { binary => 1 , quote_space => 0 } ) or die "cannot use csv: ".text::csv->error_diag (); while ( <> ) { $csv->print( \*stdout, [ @{[ split /\^a/ ]}[1..5] ] ); print "\n"; }
Comments
Post a Comment