Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

grep - how to exclude fields ?

SOLVED
Go to Solution
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
1 ACCEPTED SOLUTION
Honored Contributor Honored Contributor

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
7 REPLIES
Honored Contributor Honored Contributor

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
Honored Contributor Honored Contributor

Re: grep - how to exclude fields ?

Hey

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

Regards
Acclaimed Contributor Acclaimed Contributor

Re: grep - how to exclude fields ?

HI:

The simple way:

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

Regards!

...JRF...
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.
Acclaimed Contributor 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.
Honored Contributor 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
Honored Contributor 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