1833007 Members
2931 Online
110048 Solutions
New Discussion

grep only one column

 
SOLVED
Go to solution
Maurice Skubski
Trusted Contributor

grep only one column

Hi, i want to grep only one column out of the bdf command. is there one command (bdf | grep ...) to do this. i think with awk it is possible, but i don't know anything about awk :( is there a good documentation? thanx for help.
TIP: ITO was renamed to VPO. And now with Version 7 it is renamed to OVO (OpenView Operations)
10 REPLIES 10
Ron Kinner
Honored Contributor

Re: grep only one column

Not clear exactly what you want. If you just want the one column x you can do
bdf|cut -fx

Ron
John Carr_2
Honored Contributor
Solution

Re: grep only one column


Hi

try this I think this is what you mean


bdf | awk '{ print $1 }' | grep xyz

where $1 represents the column number in bdf

cheers
John
S.K. Chan
Honored Contributor

Re: grep only one column

If you want to use awk, you can do this ..
something simple ..

# bdf|awk '{print $1}'
==> will print 1st column
# bdf|awk '{print $2}'
==> will print 2nd column .. and so on.

A good book on awk and sed is the O'Reilly book.
Maurice Skubski
Trusted Contributor

Re: grep only one column

sorry, both things does not work. i need a command where i can get only the available memory output of a bdf command (column 4):

avail
89711
44471
230522
1847582

and only this part. any idea?
TIP: ITO was renamed to VPO. And now with Version 7 it is renamed to OVO (OpenView Operations)
S.K. Chan
Honored Contributor

Re: grep only one column

Have you tried ..

# bdf|awk '{print $4}'
Maurice Skubski
Trusted Contributor

Re: grep only one column

oh, i replaced the $1 :(
now it is working. thak you for help!!!!
TIP: ITO was renamed to VPO. And now with Version 7 it is renamed to OVO (OpenView Operations)
Darrell Allen
Honored Contributor

Re: grep only one column

Hi,

You can use:
bdf | awk '/some_text/ {print $1}'

The result would be as if you only printed the first column from:
bdf | grep some_text

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
Bill Hassell
Honored Contributor

Re: grep only one column

Simple answer:

bdf | awk '{print $4}'

No grep needed since you are looking just for the 4th field where a field is separated by whitespace. However, this will FAIL if the bdf is split scross 2 lines (as in a long NFS mountpoint). In that case, you'll need something a bit more sophisticated:

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

The advantage of the above code is that it will handle a 2-line bdf and also extracts all the rest of the columns, not just $AVAIL.


Bill Hassell, sysadmin
John Carr_2
Honored Contributor

Re: grep only one column

Hi again

I bet now you wish to omit the column heading and you can do that by using grep -v

bdf | awk '{ print $4 }' | grep -v avail

Ron's answer was also correct and simpler.

bdf | cut -f4 | grep -v avail

cheers
John
TrustNo1
Regular Advisor

Re: grep only one column

Bill,
I wasn't in on this thread but... I wanted to thank you for the valuable learning experience you provided in your "Sophisticated Code" example.
GREAT STUFF! :)
~jdk
Dare to Dream