1834786 Members
3301 Online
110070 Solutions
New Discussion

scripting advice

 
SOLVED
Go to solution
Steve Massey_1
Frequent Advisor

scripting advice

Hi

I am developing an interactive monitor script. It works fine, but I am looking for a more subtle use of awk/sed in the disp() function.
I am hoping to replace the amount of greping being done.
Is there a better way ??

Thanks for any help

Steve
6 REPLIES 6
Dan Hetzel
Honored Contributor

Re: scripting advice

Hi Steve,

You could combine the 2 first 'grep' into one:

grep "LV Name" /tmp/vgout | awk -F/ '{print $3,$4}' | read vgp lvgp

My 2 cents,

Dan
Everybody knows at least one thing worth sharing -- mailto:dan.hetzel@wildcroft.com
Dan Hetzel
Honored Contributor

Re: scripting advice

Hi again,

Another line, reading:

echo $vgp $lvgp $mirr1 $stat1 | awk '{printf " %-10s %-10s %-10s %-10s\n",$1, $2, $3, $4}'

could be replaced with:

printf " %-10s %-10s %-10s %-10s\n" $vgp $lvgp $mirr1 $stat1


That makes 4 cents !! ;7)

Dan

Everybody knows at least one thing worth sharing -- mailto:dan.hetzel@wildcroft.com
Dan Hetzel
Honored Contributor
Solution

Re: scripting advice

Hi,

You could use the 'line search' feature of 'awk' to find all the strings you're looking for in displ. Instead of running 4 'grep' and 4 'awk' programs, you could combine this in one simple 'awk' outputting 4 values that is piped into a read statement.

6 cents, still far from a full buck !

Dan



Everybody knows at least one thing worth sharing -- mailto:dan.hetzel@wildcroft.com
Andreas Voss
Honored Contributor

Re: scripting advice

Hi,

here my version of the function displ()

displ()
{
/usr/sbin/lvdisplay -v $b 2>/dev/null | awk '{
if($0 ~ "LV Name")
{
split($0, a, "/");
vgp=a[3];
lvgp=a[4];
}
if($0 ~ "LV Status")
{
if($3 == "available/syncd")
status="Good";
else
status="Bad";
}
if($0 ~ "Mirror copies")
{
if($3 == "1")
mirror="Yes";
else
mirror="No";
printf(" %-10s %-10s %-10s %-10s\n", vgp, lvgp, mirror, status);
}
}'
}

Regards
Steve Massey_1
Frequent Advisor

Re: scripting advice

Guys

- looks good to me

Thanks for your help

Steve
Shannon Petry
Honored Contributor

Re: scripting advice

As Andreas so nicely shows, awk can be used to do many tasks. Awk understands regular expressions, line and body searches, and prints very nicely with "printf".
Any time your are taking output from grep and piping to awk, the grep can be omitted. (Maybe a rare case where it can not, but I have never seen any...)
I.E.
ps -ef|grep inetd|awk '{print $2}
can be replaced with
ps -ef|awk '/"inetd"/ {print $0}'
NOTE: Certain shells will give you the awk process as well.....But I think you get the point....


Best regards,
Shannon

PS. O'Reilly and Associates puts out a great book on sed and awk. If you dont have it, it is definately worth the $30 bucks or so!
Microsoft. When do you want a virus today?