1844184 Members
2368 Online
110229 Solutions
New Discussion

awk Query.

 
RAC_1
Honored Contributor

awk Query.

I am doing following awk command and get output as follows.

ssh -q abcxyz 'bdf /var /tmp'|awk -v sd=nxdrpap3 '{print sd} /^\/dev/ {print $0}'
abcxyz
abcxyz
/dev/vg00/lvol7 2048000 1705778 321956 84% /var
abcxyz
/dev/vg00/lvol5 1024000 542751 452955 55% /tmp


I want it to be as follows.
abcxyz
/dev/vg00/lvol7 2048000 1705778 321956 84% /var
/dev/vg00/lvol5 1024000 542751 452955 55% /tmp

How do I put awk statement then?
There is no substitute to HARDWORK
16 REPLIES 16
Sandman!
Honored Contributor

Re: awk Query.

Place the BEGIN construct before the hostname (I assume) modifying it as...

ssh -q abcxyz 'bdf /var /tmp'|awk -v sd=nxdrpap3 'BEGIN {print sd} /^\/dev/'
RAC_1
Honored Contributor

Re: awk Query.

How to add some checking? Print only if bdf is more than 50% used.
There is no substitute to HARDWORK
Sandman!
Honored Contributor

Re: awk Query.

ssh -q abcxyz 'bdf /var /tmp'|awk -v sd=nxdrpap3 'BEGIN {print sd} /^\/dev/{if($(NF-1)>50)print $0}'
RAC_1
Honored Contributor

Re: awk Query.

This would print abcxyz, even if no bdfs are more than 50%. (for example.)

Above, I want FS more than 85%, and there are none. But your awk contruct will still print abcxyz. I do not want that. If no FSs are more than 85%, it should not print anything at all, Else print hostname and FSs that are more than threshold specified.
There is no substitute to HARDWORK
Sandman!
Honored Contributor

Re: awk Query.

Okay I finally understand what you're trying to get at. Looks like you will need a multi-pipeline of awk's i.e.

ssh -q awhq7135 'bdf /var /tmp' | awk '/^\/dev/{if($(NF-1)>90)print $0}' |
awk -v sd=nxdrpap3 '{if(NR==1)print sd"\n"$0;else print $0}'

The first awk pipe filters out the FS based on the >50% criterion and the second awk pipeline prints out the hostname and those FS's that are >50% capacity otherwise it prints nothing.

hope it helps!
Sandman!
Honored Contributor

Re: awk Query.

Wrong criteria (>90) in the last posting, instead use the one below...

ssh -q abcxyz 'bdf /var /tmp' | awk '/^\/dev/{if($(NF-1)>50)print $0}' |
awk -v sd=nxdrpap3 '{if(NR==1)print sd"\n"$0;else print $0}'
RAC_1
Honored Contributor

Re: awk Query.

In fact, I am doing following. I need output only if wait and avserv is greate than 250 and %busy greater than 50%. I am using follwoing, but it prints following also. It should print only if above all conditions are met. In such case, it should print host name and data. (data if wait and aserv greater than 250 and %busy greater than 50)

Wrong outout-
lmnop
00:00:01 00:00:01 device %busy avque r+w/s blks/s avwait avserv
02:30:00 c55t3d6 76.55 17.41 36 3311 253.57 101.92

abcxyz
00:00:00 00:00:00 device %busy avque r+w/s blks/s avwait avserv

I should get only
lmnop
00:00:01 00:00:01 device %busy avque r+w/s blks/s avwait avserv
02:30:00 c55t3d6 76.55 17.41 36 3311 253.57 101.92

I am using following.
for i in lmnop abcxyz
do
remsh ${i} 'cat /var/adm/sa/sar02'|/usr/bin/sed -n '/device/,/Average/p'|egrep -iv "^$|Average"|awk '/^[0-9][0-9
]:/{t=$1} /avserv$/;{if (NR==1 || (NF==8 && $(NF-1)>250) || (NF==7 && $(NF-1)>250)) print t,$0}'|egrep -v '^[0-
9][0-9]:[0-9][0-9]:[0-9][0-9] device'|awk '{if ($NF>50) print $0}'|awk '{if ($3>50) print $0}'|awk -v sd=${i}
'{if(NR==1)print sd"\n"$0"\n";else print $0}'
done
There is no substitute to HARDWORK
Peter Nikitka
Honored Contributor

Re: awk Query.

Hi,

to output conditionally, collect your data in an array. My example is based on the original bdf-question:

bdf -l |
awk -v h=hp9 -v lim=85 '/^\/dev/ {lev=substr($(NF-1),1,length($(NF-1)-1))+0;
if(lev>lim) l[++n]=$0}
END {if(n) {print h;for(j=1;j<=n;j++) print l[j]}}'

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Peter Nikitka
Honored Contributor

Re: awk Query.

Hi (again),

after reviewing your request of sar-data, I suggest to perform the whole task in awk (set your busy+avwait limits to your needs):

for h in host1 host2
do
remsh $h -n cat /var/adm/sa/sar02 |
awk -v busy=5 -v wait=7 -v h=$h '$2 == "device" {report=1;header=$0;next}
/^Average/ {report=0;next}
NF && report {if(($(NF-5)+0 >busy) || ($(NF-1)+0 >wait)) l[++n]=$0 }
END {if(n) {print h;print header;for(j=1;j<=n;j++) print l[j]}}'
done

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Hein van den Heuvel
Honored Contributor

Re: awk Query.


As you go, remember the header lines.

Then when the reporting condition is triggered, print the header if not yet printed and print the trigger line.

Sticking to the df example, using the sd header variable itself as the flag:

# df
Filesystem 1k-blocks Used Available Use% Mounted on
/dev/hda2 60928 60928 0 100% /
/dev/hda3 54405 43810 10595 81% /mnt
/dev/hda1 1743 1571 172 90% /boot
/dev/loop0 979 15 914 2% //mnt/root # df | awk -v sd=abc '/Use%/ {sd = sd "\n" $0} /^.dev/ && ($(NF-1) > 99) { if (sd) { print sd; sd=""} print} '
# df | awk -v sd=abc '/Use%/ {sd = sd "\n" $0} /^.dev/ && ($(NF-1) > 80) { if (sd) { print sd; sd=""} print} '
abc
Filesystem 1k-blocks Used Available Use% Mounted on
/dev/hda3 54405 43810 10595 81% /mnt
/dev/hda1 1743 1571 172 90% /boot

Using a seperate flag this looks like:

df | awk -v sd=abc '/Use%/ {sd = sd "\n" $0} /^.dev/ && ($(NF-1) > 80) { if (!header++) { print sd } print} '


That is, when the condition is met, test and increment header. If it was not true (0) then print header text. From now on it is no longer zero no matter how many more lines print.

hth,
Hein.



Anil C. Sedha
Trusted Contributor

Re: awk Query.

My two cents

bdf|awk -v PCT=60 'NR==1||substr($5,1,index($5,"%")-1)+0>=PCT' >> daily_${dcheck
}_$(hostname).log

(This will show all filesystems that are in use above 60%)

No points pls. This is what i used from one of our members here on the forums sometime back.
If you need to learn, now is the best opportunity
Arturo Galbiati
Esteemed Contributor

Re: awk Query.

Hi,
I'm the author:
# Show mount points over PCT
bdf|awk -v PCT=90 'NR==1||substr($5,1,index($5,"%")-1)+0>=PCT'

HTH,
Art
Urszula Gronkiewicz
New Member

Re: awk Query.

Hi Arturo, why +0 is needed here.
bdf|awk -v PCT=90 'NR==1||substr($5,1,index($5,"%")-1)+0>=PCT'

I found the above posting very useful and really nice for what I needed to do but do not get why the +0 is needed. works the same without +0.Could you please explain it?
Thank you
Dennis Handly
Acclaimed Contributor

Re: awk Query.

>Urszula: Hi Arturo, why +0 is needed here.

Peter had it first.

>but do not get why the +0 is needed. works the same without +0.

Two possible reasons.
1) To convert to numeric so a string compare isn't done.
2) To handle the case of substr returning an empty string?
Yogeeraj_1
Honored Contributor

Re: awk Query.

hi,

just out of curiosity, i ran the command and this does not seem to work!!

SRV2:>bdf /d01
Filesystem kbytes used avail %used Mounted on
/dev/vgappinst/lv_appinst
20963328 20062972 900356 96% /d01
SVR2:>bdf|awk -v PCT=90 'NR==1||substr($5,1,index($5,"%")-1)+0>=PCT'
Filesystem kbytes used avail %used Mounted on
SVR2:>

did i miss anything?

kind regards
yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
Yogeeraj_1
Honored Contributor

Re: awk Query.

hi again,

I think i found my mistake!

When the row of the bdf output for a specific file system spans over more than 1 line, it does not work!

tried the same but with bdfmegs

SRV2:home/yogeeraj>./bdfmegs|awk -v PCT=90 'NR==1||substr($5,1,index($5,"%")-1)+0>=PCT'
File System Mbytes Used Avail %Used Mounted on
/dev/vgpfsdb01/lv_pfsdb01 20472 20365 105 99% /d10
/dev/vgappinst/lv_appinst 20472 19593 879 96% /d01
/dev/vgorarestore/lv_orarestore 35828 33831 1997 94% /RESTORE
SRV2:home/yogeeraj>

kind regards
yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)