Operating System - HP-UX
1758670 Members
2299 Online
108874 Solutions
New Discussion юеВ

Re: Script help - reading mulple lines into variables from command execution

 
SOLVED
Go to solution
Jack C. Mahaffey
Super Advisor

Script help - reading mulple lines into variables from command execution

I want to be able to execute a command, do some greps on the output and then assign the output columns to variables within a loop. There's got to be a way to do this without saving the ouput to a file and then reading the file.

Any examples to share?

Thanks...
Jack

----------------------------------

Example:
while read -r field1 field2 field3 field4 field5 field6 field7 field8 field9 field10
do
echo $field1
done < lanscan | grep -v "^Path" | grep -v "^Hardware" | awk ' { print $1 " " $2 " " $3 " " $4 \ " " $5 " " $6 " "
$7 " " $8 " " $9 " " $10 } '
---------------------------------

10 REPLIES 10
harry d brown jr
Honored Contributor
Solution

Re: Script help - reading mulple lines into variables from command execution

How about a little perl?

#!/usr/bin/perl
open(INFILEPTR,"lanscan |") || die "unable to find lanscan";
while () {
next if (grep(/^Path/,$_));
next if (grep(/^Hardware/,$_));
tr/ / /s;
($field1, $field2, $field3, $field4, $field5, $field6, $field7, $field8, $fie
ld9, $field10) = split(/ /);
print $field1,"_" ,$field2,"_", $field3,"_", $field4,"_", $field5,"_", $field6,"_", $field7,"_", $field8,"_", $field9,"_", $field10;
}


live free or die
harry
Live Free or Die
Jack C. Mahaffey
Super Advisor

Re: Script help - reading mulple lines into variables from command execution

Thanks Harry.. Love the script. I'd also like to get the shell version also. I'll be using the perl version.

Jack...
curt larson_1
Honored Contributor

Re: Script help - reading mulple lines into variables from command execution

how about trying it this way

lanscan |
grep -v "^Path" |
grep -v "^Hardware" |
awk ' { print $1 " " $2 " " $3 " " $4 \ " " $5 " " $6 " "$7 " " $8 " " $9 " " $10 } ' |
while read -r field1 field2 field3 field4 field5 field6 field7 field8 field9 field10
do
echo $field1
done

instead of all the greps and awk you could just do
awk '
/^Path/ {next;}
/^Hardware/ {next;}
{ print $1 " " $2 " " $3 " " $4 \ " " $5 " " $6 " "$7 " " $8 " " $9 " " $10 } '

or

while read -r field1 field2 field3 field4 field5 field6 field7 field8 field9 field10
do
echo $field1
done < $(lanscan | grep -v "^Path" | grep -v "^Hardware" | awk ' { print $1 " " $2 " " $3 " " $4 \ " " $5 " " $6 " "
$7 " " $8 " " $9 " " $10 } ')


Jack C. Mahaffey
Super Advisor

Re: Script help - reading mulple lines into variables from command execution

Curt, your first example worked. The second did not. I might have a typo somewhere.

Jack
Geoff Wild
Honored Contributor

Re: Script help - reading mulple lines into variables from command execution

second way works if you remove the
\
before the $5

Rgds...Geoff

lanscan | awk ' ^J/^Path/ {next;}^J/^Hardware/ {next;}^J{ print $1 " " $2 " " $3 " " $4 " " $5 " " $6 " "$7 " " $8 " " $9 " " $10 } '

Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Jack C. Mahaffey
Super Advisor

Re: Script help - reading mulple lines into variables from command execution

I caught the backslash... Still didn't work. I'll check it again.
curt larson_1
Honored Contributor

Re: Script help - reading mulple lines into variables from command execution

no i couldn't get the second example to work either, but here is another way

lanscan | grep -v "^Path" | grep -v "^Hardware" | awk ' { print $1 " " $2 " " $3 " " $4 " " $5 " " $6 " "
$7 " " $8 " " $9 " " $10 } ' |&


while read -rp field1 field2 field3 field4 field5 field6 field7 field8 field9 field10
do
echo $field1
done

hey 2 out of 3 ain't bad
Jack C. Mahaffey
Super Advisor

Re: Script help - reading mulple lines into variables from command execution

I like the last example also. What specifically does the "&" and -p do? Is & a reserved variable?


Jack C. Mahaffey
Super Advisor

Re: Script help - reading mulple lines into variables from command execution

I noticed that both |& need concatenated with no space in order for it to work.