Skip to main content
Normalized `sh -c` syntax.
Source Link

This is grossly inefficient, as it results in spawning a new grep process for each input file, but:

Given a list of files (e.g., *), do

for f in file_list
do
    grep --label="$f " --with-filename pattern < "$f"
done

--label=string says, in in effect, “When reading standard input, pretend that the input filename is string.”  --with-filename (-H for short) says, “Print the file name for each match.”  This is the default when there is more than one file to search.  IMNSHO, it should also be on by default when you specify --label, but it doesn’t seem to work that way.  Since my code (above) adds a space to the filename in the argument to --label, you get output that looks like

filename :Line matching pattern

If your filenames are coming from find, do

find (path…) (expression) -exec sh -c 'grep --label="$1 " -H pattern < "$1"' _sh {} ";"

This is grossly inefficient, as it results in spawning a new grep process for each input file, but:

Given a list of files (e.g., *), do

for f in file_list
do
    grep --label="$f " --with-filename pattern < "$f"
done

--label=string says, in effect, “When reading standard input, pretend that the input filename is string.”  --with-filename (-H for short) says, “Print the file name for each match.”  This is the default when there is more than one file to search.  IMNSHO, it should also be on by default when you specify --label, but it doesn’t seem to work that way.  Since my code (above) adds a space to the filename in the argument to --label, you get output that looks like

filename :Line matching pattern

If your filenames are coming from find, do

find (path…) (expression) -exec sh -c 'grep --label="$1 " -H pattern < "$1"' _ {} ";"

This is grossly inefficient, as it results in spawning a new grep process for each input file, but:

Given a list of files (e.g., *), do

for f in file_list
do
    grep --label="$f " --with-filename pattern < "$f"
done

--label=string says, in effect, “When reading standard input, pretend that the input filename is string.”  --with-filename (-H for short) says, “Print the file name for each match.”  This is the default when there is more than one file to search.  IMNSHO, it should also be on by default when you specify --label, but it doesn’t seem to work that way.  Since my code (above) adds a space to the filename in the argument to --label, you get output that looks like

filename :Line matching pattern

If your filenames are coming from find, do

find (path…) (expression) -exec sh -c 'grep --label="$1 " -H pattern < "$1"' sh {} ";"
Source Link

This is grossly inefficient, as it results in spawning a new grep process for each input file, but:

Given a list of files (e.g., *), do

for f in file_list
do
    grep --label="$f " --with-filename pattern < "$f"
done

--label=string says, in effect, “When reading standard input, pretend that the input filename is string.”  --with-filename (-H for short) says, “Print the file name for each match.”  This is the default when there is more than one file to search.  IMNSHO, it should also be on by default when you specify --label, but it doesn’t seem to work that way.  Since my code (above) adds a space to the filename in the argument to --label, you get output that looks like

filename :Line matching pattern

If your filenames are coming from find, do

find (path…) (expression) -exec sh -c 'grep --label="$1 " -H pattern < "$1"' _ {} ";"