Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

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.

share|improve this answer
1  
Thank you very much for the answer - now it makes sense! May I just add, for others, that \n in the string stands for newline when using echo [I wasn't sure myself and this confused a bit]. –  Chirp. Not Luke. May 6 '13 at 18:13
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.