Simpler Navigation for Servers and Operating Systems - Please Update Your Bookmarks
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
If you have bookmarked forums or discussion boards in Servers and Operating Systems, we suggest you check and update them as needed.
Languages and Scripting
cancel
Showing results for 
Search instead for 
Did you mean: 

grep - how to exclude fields ?

SOLVED
Go to solution
Piotr Kirklewski
Super Advisor

grep - how to exclude fields ?

Hi there

bdf |grep /stand

output:

/dev/vg00/lvol1 295024 59344 206176 22% /stand

I nead o exclude all but lvol1 and /stand

Can anyone help ?

Cheers
Jesus is the King
7 REPLIES
john korterman
Honored Contributor
Solution

Re: grep - how to exclude fields ?

Hi,


try something like this:

$ bdf | grep stand| awk '{print $1,$NF}'

regards,
John K.
it would be nice if you always got a second chance
Oviwan
Honored Contributor

Re: grep - how to exclude fields ?

Hey

bdf | grep /stand | awk ' { print $1" "$NF}'

Regards
James R. Ferguson
Acclaimed Contributor

Re: grep - how to exclude fields ?

HI:

The simple way:

# bdf|grep -v -e /stand -e lvol1

Regards!

...JRF...
Hein van den Heuvel
Honored Contributor

Re: grep - how to exclude fields ?

With grep you get whole lines.
No ifs or buts.
So grep needs help, typically awk or cut.
John K, shows a fine example of this helping.

However, why not let awk (or perl) do it all?

$ bdf | awk '/stand/ {print $1,$NF}'

Also, somehow I'm thinking this is not your end goal. What problem are you really trying to solve ?!

Regards,
Hein.
A. Clay Stephenson
Acclaimed Contributor

Re: grep - how to exclude fields ?

... and to fix one other problem that you haven't yet anticipated, I suggest that you use bdfmegs rather than bdf. Bdfmegs is a Bill Hassell script that almost always outputs like bdf. The "almost always" is the significant part. Search the Forums for bdfmegs and you should have no trouble finding it.
If it ain't broke, I can fix that.
TwoProc
Honored Contributor

Re: grep - how to exclude fields ?

to get JUST the output:

lvol1 stand

run:

bdf | grep /stand | sed -e "s/\// /g" -e "s/ */ /g" | cut -d " " -f 4,9

Note: the first sed expression has a single space before the "/g", and the second one has *two spaces* after the "s/" and a single space before the "/g".

I'm guessing that since you know that /stand is in vg00 you're not interested in the whole /dev/vg00/lvol1 part of the string.

We are the people our parents warned us about --Jimmy Buffett
Highlighted
TwoProc
Honored Contributor

Re: grep - how to exclude fields ?

Lots of ways to get that done, mainly because there are a number of sources for that information: commands "df" and "bdf", as well as files "/etc/fstab" and "/etc/mnttab" .

An easier one than the previous posting would be:

> df | grep stand | cut -b -15,33-42

And actually
> df -n | grep stand

gets you pretty close to what you asked for w/o stream edits.


If you're wanting make sure to avoid the "bdf" line wrap problem indirectly mentioned in a previous posting use the contents /etc/mnttab instead:

> grep stand /etc/mnttab | cut -d "/" -f 4,5 | cut -d " " -f 1,2

gives:

lvol1 /stand

We are the people our parents warned us about --Jimmy Buffett