What I am after is almost exactly the same as can be found here, but I want the format "line number, separator, filename, newline" in the results, thus displaying the line number at the beginning of the line, not after the filename, and without displaying the line containing the match.
The reason why this format is preferable is that
- (a) the filename might be long and cryptic and contain the separator which the tool uses to separate the filename from the line number, making it incredibly difficult to use awk to achieve this, since the pattern inside the file might also contain the same separator. Also, line numbers at the beginning of the line will be aligned better than if they appear after the filename. And the other reason for this desired format is that
(b) the lines matching the pattern may be too long and mess up the one line per row property on the output displayed on standard out (and viewing the output on standard out is better than having to save to a file and use a tool like vi to view one line per row in the output file).
Now that I've set out the requirement, consider this:
Ack is not installed on the Linux host I'm using, so I cannot use it.
If I do the following, the shell executes
find .
and substitutes 'find .` with a list of absolute paths starting at the current working directory and proceeding downwards recursively:grep -n PATTERN $(find .)
then the -n prints the line number, but not where I want it. Also, for some reason I do not understand, if a directory name includes the PATTERN, then grep matches it in addition to the regular files that contain the pattern. This is not what I want, so I use:
grep -n PATTERN $(find . -type f)
I also wanted to change this command so that the output of find is passed on to grep dynamically. Rather than having to build the entire list of absolute paths first and then pass the bulk of them to grep, have find pass each line to grep as it builds the list, so I tried:
find . -exec grep -n PATTERN '{}' \;
which seems like the right syntax according to the
man page
but when I issue this command the Bash shell executes about 100 times slower, so this is not the way to go.
In view of what I described, how can I execute something similar to this command and obtain the desired format. I have already listed the problems associated with the related post.