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


In reply to Re: Extracting multiple column from csv file and adding/appending onto new csv file with first columns already existing by kcott
in thread Extracting multiple column from csv file and adding/appending onto new csv file with first columns already existing by bks

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.