Operating System - HP-UX
1833757 Members
2331 Online
110063 Solutions
New Discussion

How can I get awk to distinguish between columns

 
SOLVED
Go to solution
Donald C Nelson
Frequent Advisor

How can I get awk to distinguish between columns

I have the following bdf output:

%kbytes %used %avail
/dev/vg06/testdata4
10485760 8927978 1460428
/dev/vg06/testdata3
7340032 2894101 4168167
/dev/vg06/testdata2
7340032 3545317 3557638

/dev/vg06/mw 1001729 259606 641950

An awk '{print$2}' will only give the %kbyte size for /vg06/mw as the 2nd column output. The %kbytes column for the other longer filesystems shows as {print $1}. What do I have to do to get $2 to print out the %kbytes for each filesystems no matter how long the FS is?
9 REPLIES 9
RAC_1
Honored Contributor

Re: How can I get awk to distinguish between columns

bdf|awk -F " " '{print $2}'
Does it print what you want??
There is no substitute to HARDWORK
Donald C Nelson
Frequent Advisor

Re: How can I get awk to distinguish between columns

RAC, it still only prints the $2 for the shorter filesystem names.
RAC_1
Honored Contributor

Re: How can I get awk to distinguish between columns

Check following thread.

http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=847833

Also post your bdf_output|od -c
There is no substitute to HARDWORK
Hein van den Heuvel
Honored Contributor

Re: How can I get awk to distinguish between columns


This question has been answerred a few times before. Unfortunately the (advanced) search in the forum is not helpful. Try to google for:
+hpux +df +awk +site:itrc.hp.com



On the data you showed (which is unlike bdf data I've seen before) this worked:

awk '{if (NF==4) {dev=$1}; if (NF==1) {dev=$1}; if (NF>2) {print dev,$(NF - 2)}}'


Good luck,
Hein.

Bill Hassell
Honored Contributor

Re: How can I get awk to distinguish between columns

bdf is a real pqain for scripting because it breaks long lines into 2 lines. In your script, do a read for each of the items (much simpler and awk is really slow for this task), then see if the total column is null. If true, then read again for the rest of the options:

bdf | while read FS TOT USED AVAIL PERCENT MNT
do
if [ "$TOT" = "" ]
then
read TOT USED AVAIL PERCENT MNT
fi
echo "params are $FS $TOT $USED $AVAIL $PERCENT $MNT"

Now you have all 6 parameters regardless of whether the bdf entry is one line or two. Now you can add or test values.


Bill Hassell, sysadmin
Jdamian
Respected Contributor

Re: How can I get awk to distinguish between columns

Your awk program should detect those broken lines and read the next line.

bdf | awk '
{
if (NF<7) { COL1=$1 ; getline; COL2=$1 }
else { COL1=$1 ; COL2=$2 }

print COL1 " " COL2
}'
Bill Hassell
Honored Contributor
Solution

Re: How can I get awk to distinguish between columns

Just noticed a missing line....add: done
to the end of my example, right under the echo.


Bill Hassell, sysadmin
Kent Ostby
Honored Contributor

Re: How can I get awk to distinguish between columns

create useme.awk with the following contents:

BEGIN{daflag=0;}
daflag==1 {$0=dahold" "$0;daflag=0;}
NF>2 {printf("%25s %10d\n", $1,$2);next};
{daflag=1;dahold=$0;}

Then run:

bdf | awk -f useme.awk

The output will give you:

/dev/vg06/testdata4 10485760
/dev/vg06/testdata3 7340032
/dev/vg06/testdata2 7340032
/dev/vg06/mw 1001729

Essentially the script combines lines that are "too small" and then prints $1 and $2 for lines that were long enough or lnies that have been combined.
"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"
Donald C Nelson
Frequent Advisor

Re: How can I get awk to distinguish between columns

Kent solution was perfect. Thanks Kent.