Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Perl solution

by Anonymous Monk
on Jan 27, 2008 at 19:37 UTC ( #664552=perlquestion: print w/replies, xml ) Need Help??

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

I want to take the output of netsat -e and output it to a .csv file.

Replies are listed 'Best First'.
Re: Perl solution
by shmem (Chancellor) on Jan 27, 2008 at 19:54 UTC
    The CoC* service responds: insert coin here.

    Seriously, what problems do you have, what did you try, did you read the PerlMonks FAQ and not stumble over How do I post a question effectively? Hard to believe.

    For a starter,

    netstat -e | perl -ple 's/\s\s+/\t/' > file.csv

    writes the output of netstat -e into a tab delimited csv file. You have to tweak the regular expression used in that snippet according to the spec.

    What spec? Ah, uhm, wait, er...

    *) Crank out Code


    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
      netstat -e | perl -ple 's/\s\s+/\t/' > file.csv

      Very likely an intended g modifier is missing from that s///. Of course, without a problem specification, who can say for sure that the g is needed? :)

      a tab delimited csv file

      I think you mean "a tsv file". A csv file is, by definition, comma delimited. A better version would probably be something like this:

      netstat -e | perl -ple 's/\s\s+/,/' > file.csv

      . . . and, of course, neither of our versions are exactly perfect/complete.

      print substr("Just another Perl hacker", 0, -2);
      - apotheon
      CopyWrite Chad Perrin

        You are certainly not wrong, but it could be argued that csv became a 'backronym' of Character Separated Values.

        How can you feel when you're made of steel? I am made of steel. I am the Robot Tourist.
        Robot Tourist, by Ten Benson

      ** or Cockup Operations Corrector
Re: Perl solution
by citromatik (Curate) on Jan 28, 2008 at 16:28 UTC

    Alternatively, you can feed your script with the output of netstat -e opening a pipe:

    use strict; use warnings; open NETSTAT, "netstat -e |" or die $!; while (<NETSTAT>){ # process the line # Output to csv } close NETSTAT;
Re: Perl solution
by Anonymous Monk on Jan 27, 2008 at 19:38 UTC
    typo in netstat

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://664552]
Approved by ww
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2020-02-25 02:17 GMT
Find Nodes?
    Voting Booth?
    What numbers are you going to focus on primarily in 2020?

    Results (108 votes). Check out past polls.