How to show a 'grep' result with the complete path or file name
Assuming you have two log-files in:
- C:/temp/my.log
- C:/temp/alsoMy.log
'cd' to C: and use:
grep -r somethingtosearch temp/*.log
It will give you a list like:
temp/my.log:somethingtosearch
temp/alsoMy.log:somethingtosearch1
temp/alsoMy.log:somethingtosearch2
How to grep only show path/filename?
grep -l
will output JUST the names of files which matched, without showing the actual match.
grep to match filename in full path
awk
, getting the /
separated last field:
% awk -F/ '{print $NF}' <<<'/etc/network/interfaces'
interfaces
% awk -F/ '{print $NF}' <<<'/home/user/Documents/report.pdf'
report.pdf
grep
, getting the portion after last /
:
% grep -o '[^/]\+$' <<<'/etc/network/interfaces'
interfaces
% grep -o '[^/]\+$' <<<'/home/user/Documents/report.pdf'
report.pdf
sed
, replacing everything upto the last /
with null:
% sed 's_.*/__' <<<'/etc/network/interfaces'
interfaces
% sed 's_.*/__' <<<'/home/user/Documents/report.pdf'
report.pdf
How can I extract a file name from a grep result?
OK, thanks to the help from alb3rtobr, I was able to get it:
egrep -hor '(^\s*/|\$\(\s*/|\`\s*/)[^ ]*' /etc/rc.d/* /etc/init.d | sed -e 's/\($(\|)\|^\s*\)//g' | sort | uniq
I modified the egrep to continue matching until encountering a space and then added the -o
option to return only the matched pattern. I also modified the sed to trim leading whitespace.
EDIT: Changed to match pattern while not a space character
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.
How to show the full path for a file found using the find command and given to grep to match a string
Move the -print
to the right-hand end so it is "gated" by the outcome of exec
:
find `pwd -P` -name config.php -exec grep 'assetBasePath.*cloudfront' {} \; -print
Show filename and line number in grep output
I think -l
is too restrictive as it suppresses the output of -n
. I would suggest -H
(--with-filename
): Print the filename for each match.
grep -Hn "search" *
If that gives too much output, try -o
to only print the part that matches.
grep -nHo "search" *
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.
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.
Related Topics
How to Pass Argument in Expect Through the Command Line in a Shell Script
Keep Remote Directory Up-To-Date
How to Write a Linux Daemon with .Net Core
Docker: Are You Trying to Connect to a Tls-Enabled Daemon Without Tls
Importing a Cmake Project into Eclipse Cdt
Linux: Merging Multiple Files, Each on a New Line
Compress Files While Reading Data from Stdin
How to Check the Bios Version or Name in Linux Through a Command Prompt
Linux Script with Curl to Check Webservice Is Up
Linux: How to Know the Module That Exports a Device Node
How to Load Luks Passphrase from Usb, Falling Back to Keyboard
Forcing Driver to Device Match
Should I Rebuild a Dependent Lib After System Update
How to Generate PDF from Docbook 5.0
How to Fetch Java Version Using Single Line Command in Linux
Getting Stacktrace from Core Dump
How to Know the Interrupt/Gpio Number for a Specific Pin in Linux