Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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

by kcott (Archbishop)
on Apr 18, 2014 at 01:06 UTC ( [id://1082694]=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

G'day bks,

Welcome to the monastery.

"I would be getting similar output from different devices and i would like append the output to same csv file"

I don't know which of the two CSV files this refers to. Use a mode of '>' to write to a new file; and a mode of '>>' to append to an existing file. See open for details. I've used append mode for both files in the code below.

"I tried installing text::csv_xs, but unfortunately i wasnt able extract *.tgz as it gave some checksum error. Please guide me or point me in right direction about how to install modules that are in *.tgz format"

You'll need to show us what you did and what output you got. The guidelines in "How do I post a question effectively?" explain the sort of information we need and how to present it.

"Please post some sample code to achieve my requirement that would really be of great help"

The code below shows how to use Text::CSV for all your I/O.

#!/usr/bin/env perl -l use strict; use warnings; use autodie; use Text::CSV; my ($all_csv, $sel_csv) = qw{pm_1082609_all.csv pm_1082609_sel.csv}; my $ip = '192.168.10.3'; chomp( my @output = <DATA> ); my $csv = Text::CSV::->new(); open my $out_all_fh, '>>', $all_csv; $csv->print($out_all_fh => [ split /\t/ ]) for @output; close $out_all_fh; open my $in_all_fh, '<', $all_csv; open my $out_sel_fh, '>>', $sel_csv; while (my $row = $csv->getline($in_all_fh)) { $csv->print($out_sel_fh, [ $ip, @$row[0,8] ]); } close $in_all_fh; close $out_sel_fh; __DATA__ 2.2.2.2 4 100 273 274 2 0 0 04:30:05 0 3.3.3.3 4 100 273 274 2 0 0 04:30:05 0 4.4.4.4 4 100 273 273 2 0 0 04:30:00 0

Here's the file contents after running that script.

$ cat pm_1082609_all.csv 2.2.2.2,4,100,273,274,2,0,0,04:30:05,0 3.3.3.3,4,100,273,274,2,0,0,04:30:05,0 4.4.4.4,4,100,273,273,2,0,0,04:30:00,0
$ cat pm_1082609_sel.csv 192.168.10.3,2.2.2.2,04:30:05 192.168.10.3,3.3.3.3,04:30:05 192.168.10.3,4.4.4.4,04:30:00

-- Ken

  • Comment on Re: Extracting multiple column from csv file and adding/appending onto new csv file with first columns already existing
  • Select or 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 bks (Novice) on Apr 29, 2014 at 11:41 UTC

    Thanks a lot Ken. By using your sample script i was able to extract the required columns.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (1)
As of 2024-04-25 00:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found