You are not logged in.

#1 2012-08-02 20:42:36

Gen2ly
Member
From: Sevierville, TN
Registered: 2009-03-06
Posts: 1,529
Website

[Solved] bash expertise needed

I got a array that I'd like to have expanded into multiple pipes and I can't think of anyway to do it.  I'd like to do this:

exclude=(sandfox iso)

df -h | grep -e 'Avail' -e '^/dev' | grep -v sandfox | grep -v iso

I'd like the array to go into multiple versions of grep -v.  Any thoughts on how to do this?

Last edited by Gen2ly (2012-08-03 00:22:44)


Setting Up a Scripting Environment | Proud donor to wikipedia - link

Offline

#2 2012-08-02 21:40:31

alphaniner
Member
From: Ancapistan
Registered: 2010-07-12
Posts: 2,583

Re: [Solved] bash expertise needed

I can't say it's the ideal solution, but putting

grep -vf <(printf "%s\n" "${exclude[@]}")

after the second pipe should work.  Assuming I'm understanding your snippet correctly, and you want to filter out any line containing anything from your array.

What it does: the printf command prints the array one item per line.  The <( ... ) causes the output of the command inside it to be treated as a file.  And the -f flag tells grep to obtain its patterns from this "file".

Last edited by alphaniner (2012-08-02 21:42:50)


But whether the Constitution really be one thing, or another, this much is certain - that it has either authorized such a government as we have had, or has been powerless to prevent it. In either case, it is unfit to exist.
-Lysander Spooner

Offline

#3 2012-08-02 21:41:14

jjacky
Member
Registered: 2011-11-09
Posts: 321
Website

Re: [Solved] bash expertise needed

Far from a bash expert, but if I get what you're trying to do, a solution might be to put all your terms inside call to grep, e.g:

df -h | grep -e 'Avail' -e '^/dev' | egrep -v $(echo ${exclude[@]} | tr ' ' '|')

Offline

#4 2012-08-02 22:13:03

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: [Solved] bash expertise needed

You can control the array item separation character in bash.

When you use subscript * instead of @ and it is in double quotes, the first character of IFS is used.

IFS=$'\n'
df -h | grep -v "${exclude[*]}"

Offline

#5 2012-08-02 23:22:38

Gen2ly
Member
From: Sevierville, TN
Registered: 2009-03-06
Posts: 1,529
Website

Re: [Solved] bash expertise needed

alphaniner wrote:

I can't say it's the ideal solution, but putting

grep -vf <(printf "%s\n" "${exclude[@]}")

after the second pipe should work.  Assuming I'm understanding your snippet correctly, and you want to filter out any line containing anything from your array.

What it does: the printf command prints the array one item per line.  The <( ... ) causes the output of the command inside it to be treated as a file.  And the -f flag tells grep to obtain its patterns from this "file".

You understood exactly what I was needing.

That is really neat.  I never knew that in bash <( ... ) would treat it as a file.  Very useful.  Thank you.

Procyon wrote:

You can control the array item separation character in bash.

When you use subscript * instead of @ and it is in double quotes, the first character of IFS is used.

IFS=$'\n'
df -h | grep -v "${exclude[*]}"

I think this is a little different than I was looking for.  But very good to know smile.  Thanks Procyon.


Setting Up a Scripting Environment | Proud donor to wikipedia - link

Offline

#6 2012-08-02 23:58:48

jjacky
Member
Registered: 2011-11-09
Posts: 321
Website

Re: [Solved] bash expertise needed

Procyon wrote:

When you use subscript * instead of @ and it is in double quotes, the first character of IFS is used.

Thank you for that! I knew there was a reason I couldn't get that to work (and used the ugly echo/tr), but had failed to find it (though I looked only briefly in man bash).

Offline

Board footer

Powered by FluxBB