Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Parsing output from the CSV file

by kar_333 (Initiate)
on Feb 07, 2013 at 15:53 UTC ( [id://1017681]=perlquestion: print w/replies, xml ) Need Help??

kar_333 has asked for the wisdom of the Perl Monks concerning the following question:

hello monks

I am having problem in parsing the output from the CSV file. I want to add pipe symbol in between the character to do mutliple search similar to egrep

open(my $data, '<', $Config_File) or die "Could not open '$Config_File +' $!\n"; my $reg_exp; my $severity; my @fields=(); while (my $line = <$data>) { chomp $line; if(!$line =~ /^$/) { @fields = split "," , $line; $reg_exp = $fields[0]; $severity = $fields[1]; print $reg_exp; } } #print $fields[0]; #last unless defined $line; close($data) ;

current Output

service entered the stopped stateservice entered the running state

desired output

service entered the stopped state|service entered the running state

Replies are listed 'Best First'.
Re: Parsing output from the CSV file
by Tux (Canon) on Feb 07, 2013 at 16:03 UTC
Re: Parsing output from the CSV file
by CountZero (Bishop) on Feb 07, 2013 at 19:48 UTC
    Can you show us some lines of your input CSV-file?

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
Re: Parsing output from the CSV file
by manorhce (Beadle) on Feb 07, 2013 at 19:51 UTC

    Use like the below it will parse the data and will create fields according

    $csv = Text::CSV::Simple->new({ sep_char => q{,}, binary => 1 }); $csv->field_map( qw( field1 field2 field3 field4 ) ); my @objectlist = $csv->read_file($file_name); foreach my $data (@objectlit) { print $data->{field1}."field2",$data->{field2} }
Re: Parsing output from the CSV file
by GotToBTru (Prior) on Feb 08, 2013 at 22:14 UTC
    You never introduce the | character. I would suggest replacing:
    print $reg_exp;
    with
    $accum .= "$reg_exp,";
    and then when ready to output your pipe delimited expression:
    $accum =~s/,$//; $accum =~ s/,/|/g; print $accum;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1017681]
Approved by BrowserUk
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found