Understanding grep –label=

I am looking for an explanation how grep --label=LABEL works: Maybe somebody can give me an example [or two] on what --label= is for.

I understand what grep and zgrep are supposed to do – the latter is mentioned in the entry on --label= in the info page:

… especially useful when implementing tools like `zgrep’

98% of what I found so far is copy/paste from info grep and the other two percents the command is embedded in a script which I don’t understand.

Answer

This feature makes reading the output of grep easier. If you want to check data that grep cannot read directly then you may end up using a pipe to feed grep instead of creating a temporary file which grep can read. If you don’t want a temporary file (e.g. because it would be huge) then without --label you would have the problem that grep cannot print the information in which file the match was found.

This

echo $'fubar\nbaz\nbat' | grep --label=inputfile -H a
inputfile:fubar
inputfile:baz
inputfile:bat

is equivalent to

echo $'fubar\nbaz\nbat' > inputfile
grep -H a inputfile
inputfile:fubar
inputfile:baz
inputfile:bat

Without --label the first approach would not work (i.e. not deliver the wanted output) so you would have to do something like this:

echo $'fubar\nbaz\nbat' | grep a | awk '{print "inputfile:" $0}'

But this does not offer match highlighting in the console.

Attribution
Source : Link , Question Author : erch , Answer Author : Hauke Laging

Leave a Comment