Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Extracting multiple column from csv file and adding/appending onto new csv file with first columns already existing

by Yaerox (Scribe)
on Apr 17, 2014 at 09:08 UTC ( #1082610=note: print w/replies, xml ) Need Help??


in reply to Extracting multiple column from csv file and adding/appending onto new csv file with first columns already existing

I can't follow where Host/Device IP is cooming from. I guess this shows that you want to merge two csv files?

I never worked on Solaris 10, but why not looking for .tar.gz and then using tar xzf (or something like that) to extract? A second way if installed would be cpan. get module, make module, test module, install module.

Anyway, if you're able to extract one column, you just have to add the same part again in your loop by checking for another column ... you're creating an array or append on a string all your results (formatted) for both csv-files then writing a third loop printing into csv-file.

Following code is not tested and should be more like a kind of Pseudo Code

my @aColumns1; my @aColumns2; open ( my $sData, '<', $hFile ) or die "Could not open '$hFile' $!\n"; while ( my $sLine = <$sData> ) { chomp $sLine; my @aFields = split "," , $sLine; push ( @aColumns1, $aField[1] ); push ( @aColumns2, $aField[3] ); } close ( $hFile ); my @aColumns3; my @aColumns4; @aFields = ''; open ( $sData, '<', $hFile ) or die "Could not open '$hFile' $!\n"; while ( $sLine = <$sData> ) { chomp $sLine; @aFields = split "," , $sLine; push ( @aColumns3, $aField[2] ); push ( @aColumns4, $aField[4] ); } close ( $hFile ); open ( '>', $hFile ) or die "Could not open '$hFile' $!\n"; for (...) { print $hFile "$aColumns1[x];$aColumns2[x];$aColumns3[x];$aColumns4 +[x]\n"; } close ( $hFile );

If you would like you could probably use 2D-Arrays too.

Regards

  • Comment on Re: Extracting multiple column from csv file and adding/appending onto new csv file with first columns already existing
  • Download Code

Replies are listed 'Best First'.
Re^2: Extracting multiple column from csv file and adding/appending onto new csv file with first columns already existing
by Bloodnok (Vicar) on Apr 17, 2014 at 09:30 UTC
    Yaerox, I think you'll find that the host IP is initially a script argument and is then subsequently returned by the invoked CISCO show command (IIRC :-).

    A user level that continues to overstate my experience :-))

      Yeah you're right. Found it ;-) But I think the way how to get this done can be pretty the same ... if not feel free to tell me :-)

      Thank you Sir.

        Worry not, I wasn't disputing your posited solution, merely answering the question you posed.

        A user level that continues to overstate my experience :-))

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (1)
As of 2022-07-02 17:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My most frequent journeys are powered by:









    Results (103 votes). Check out past polls.

    Notices?