When I use grep -o to search in multiple files, it outputs each result prefixed with the file name. How can I prevent this prefix? I want the results without the file names.
Asked
Active
Viewed 4.1k times
49
Gilles 'SO- stop being evil'
- 829,060
Ram Rachum
- 1,825
2 Answers
78
With the GNU implementation of grep (the one that also introduced -o) or compatible, you can use the -h option.
-h, --no-filename Suppress the prefixing of file names on output. This is the default when there is only one file (or only standard input) to search.
With other implementations, you can always concatenate the files with cat and grep that output:
cat ./*.txt | grep regexp
Or use sed or awk instead of grep:
awk '/regexp/' ./*.txt
(extended regexps like with grep -E).
sed '/regexp/!d/' ./*.txt
(basic regexps like with grep without -E. Many sed implementations now also support a -E option for extended regexps).
Stéphane Chazelas
- 544,893
sergut
- 1,969
1
With git grep, use the -h option to suppress file names:
git grep -h <pattern>
Note that the option --no-filename doesn't work with git grep.
Paul Rougieux
- 201
man grep-h, --no-filenameSuppress the prefixing of filenames on output when multiple files are searched. – rahul May 20 '15 at 14:29