Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2024-04-24 20:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found