You are not logged in.
Pages: 1
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
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
Offline
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
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.
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 . Thanks Procyon.
Setting Up a Scripting Environment | Proud donor to wikipedia - link
Offline
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
Pages: 1