regex - Bash, Netcat, Pipes, perl -
background: have simple bash
script i'm using generate csv
log file. part of bash script poll other devices on network using netcat
. netcat
command returns stream of information can pipe grep
command values need in csv
file. save return value grep
bash
variable , @ end of script, write out saved bash
variables csv
file. (simple enough.)
the change i'd make amount of netcat
commands have issue each piece of information want save off. each issued netcat
command possible values returned (so each time returns same data , burdensome on network). so, i'd use netcat
once , parse return value many times need create bash
variables can later concatenated single record in csv
file i'm creating.
specific question: using bash
syntax if pass output of netcat
command file using >
(versus current grep
method) file each entry on own line (presumably separated \n
eol record separator -- easy perl regex). however, if save output of netcat
directly bash
variable, , echo variable, of data jumbled together, cumbersome parse out (not easy).
i have played 2 options: first, think perl one-liner may solution here, i'm not sure how best execute it. pseudo code might save netcat
output a bash variable , somehow figure out how parse perl (not straight forward though).
the second option use bash
's >
, send netcat
's output file. easy process perl
, regex given \n
eol
, require opening external file , passing perl
script processing , somehow passing return value bash
script bash
variable entry csv
file.
i know i'm missing simple here. there way can force newline entry bash
variable netcat
, repeatedly run perl-one liner against variable create each of csv
variables need -- within same bash
script? sorry, long question.
the second option use bash's > , send netcat's output file. easy process perl , regex given \n eol, require opening external file , passing perl script processing , somehow passing return value bash script bash variable entry csv file.
this common idiom: save output netcat in temporary file, use grep or awk or perl or what-have-you many times necessary extract data file:
# create temporary file , arrange have # deleted when script exists. tmpfile=$(mktemp tmpxxxxxx) trap "rm -f $tmpfile" exit # dump data netcat # temporary file. nc somehost someport > $tmpfile # extract information variable `myvar` myvar=$(awk '/something/ {print $4}' $tmpfile)
that last line demonstrates how output of (in case, awk
script) variable. if using perl extract information same thing.
you write whole script in perl, might make life easier.
Comments
Post a Comment