Grep Without Showing Path/File:Line

grep without showing path/file:line

No need to find. If you are just looking for a pattern within a specific directory, this should suffice:

grep -hn FOO /your/path/*.bar

Where -h is the parameter to hide the filename, as from man grep:

-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.

Note that you were using

-H, --with-filename

Print the file name for each match. This is the default when there is
more than one file to search.

How to grep only show path/filename?

grep -l will output JUST the names of files which matched, without showing the actual match.

How to get only filenames without Path by using grep

Yet another simpler solution:

grep -l whatever-you-want | xargs -L 1 basename

or you can avoid xargs and use a subshell instead, if you are not using an ancient version of the GNU coreutils:

basename -a $(grep -l whatever-you-want)

basename is the bash straightforward solution to get a file name without path. You may also be interested in dirname to get the path only.

GNU Coreutils basename documentation

ag grep tool - How to print only the log lines without file name/path in the output?


ag --nofilename "test|temp"

Bash grep output filename and line no without matches

You were pointing it well with cut, only that you need the : field separator. Also, I think you need the first and second group. Hence, use:

grep -Hn 'pattern' files* | cut -d: -f1,2

Sample

$ grep -Hn a a*
a:3:are
a:10:bar
a:11:that
a23:1:hiya

$ grep -Hn a a* | cut -d: -f1,2
a:3
a:10
a:11
a23:1

How can I use grep to show just filenames on Linux?

The standard option grep -l (that is a lowercase L) could do this.

From the Unix standard:

-l
(The letter ell.) Write only the names of files containing selected
lines to standard output. Pathnames are written once per file searched.
If the standard input is searched, a pathname of (standard input) will
be written, in the POSIX locale. In other locales, standard input may be
replaced by something more appropriate in those locales.

You also do not need -H in this case.

Use grep --exclude/--include syntax to not grep through certain files

Use the shell globbing syntax:

grep pattern -r --include=\*.cpp --include=\*.h rootdir

The syntax for --exclude is identical.

Note that the star is escaped with a backslash to prevent it from being expanded by the shell (quoting it, such as --include="*.cpp", would work just as well). Otherwise, if you had any files in the current working directory that matched the pattern, the command line would expand to something like grep pattern -r --include=foo.cpp --include=bar.cpp rootdir, which would only search files named foo.cpp and bar.cpp, which is quite likely not what you wanted.

Update 2021-03-04

I've edited the original answer to remove the use of brace expansion, which is a feature provided by several shells such as Bash and zsh to simplify patterns like this; but note that brace expansion is not POSIX shell-compliant.

The original example was:

grep pattern -r --include=\*.{cpp,h} rootdir

to search through all .cpp and .h files rooted in the directory rootdir.

Linux find xargs command grep showing path and filename

The main problem here is that head doesn't pass on the info about what lines came from which file, so grep can pick out the matching lines but not show the file name or path. awk can do the matching and trimming to 50 lines, and you can control exactly what gets printed for each match. So something like this:

find /folder/202205??/ -type f -exec awk '/^Starting/ {print FILENAME ": " $0}; (FNR>=50) {nextfile}' {} +

Explanation: the first clause in the awk script prints matching lines (prefixed by the FILENAME, which'll actually include the path as well), and the second skips to the next file when it gets to line 50. Also, I used find's -exec ... + feature instead of xargs, just because it's a bit cleaner (and won't run into trouble with weird filenames). Terminating the -exec command with + instead of \; makes it run the files in batches (like xargs) rather than one at a time.



Related Topics



Leave a reply



Submit