如何使用完整路径或文件名显示grep结果

时间:2021-08-17 06:17:46

I need to know the complete file path when I grep.

我需要知道grep时的完整文件路径。

I use commands like

我用的命令就像

cat *.log | grep somethingtosearch

Now what I need to show the result with complete file path from where the matched result were taken out.

现在我需要用完整的文件路径显示结果,从中取出匹配的结果。

Help anyone?

帮助任何人?

9 个解决方案

#1


10  

Assuming you have two log-files in:

假设您有两个日志文件:

  • C:/temp/my.log
  • C:/temp/my.log
  • C:/temp/alsoMy.log
  • C:/temp/alsoMy.log

cd to C: and use:

cd到C:并使用:

grep -r somethingtosearch temp/*.log

It will give you a list like:

它将为您提供如下列表:

temp/my.log:somethingtosearch
temp/alsoMy.log:somethingtosearch1
temp/alsoMy.log:somethingtosearch2

#2


17  

Have you tried using the -l flag?

你尝试过使用-l标志吗?

grep -l somethingtosearch

This will return just the paths and file names where the search was found, not the whole lines where the match was made.

这将仅返回找到搜索的路径和文件名,而不是返回匹配的整行。

Use with -r flag for recursion.

与-r标志一起使用以进行递归。

#3


5  

If you want to see the full paths, I would recommend to cd to the top directory (of your drive if using windows)

如果你想看到完整的路径,我建议cd到*目录(如果使用Windows,你的驱动器)

cd C:\
grep -r somethingtosearch C:\Users\Ozzesh\temp

Or on Linux:

或者在Linux上:

cd /
grep -r somethingtosearch ~/temp

if you really resist on your file name filtering (*.log) AND you want recursive (files are not all in the same directory), combining find and grep is the most flexible way:

如果你真的抵制你的文件名过滤(* .log)并且你想要递归(文件不是全部​​在同一目录中),组合find和grep是最灵活的方式:

cd /
find ~/temp -iname '*.log' -type f -exec grep somethingtosearch '{}' \;

#4


2  

Command:

命令:

grep -rl --include="*.js" "searchString" ${PWD}

Returned output:

退货产量:

/root/test/bas.js

#5


1  

for me
grep -b "searchsomething" *.log worked as I wanted

对我来说grep -b“searchsomething”* .log按照我的意愿工作

#6


1  

The easiest way to print full paths is replace relative start path with absolute path:

打印完整路径的最简单方法是用绝对路径替换相对起始路径:

grep -r --include="*.sh" "pattern" ${PWD}

#7


1  

I fall here when I was looking exactly for the same problem and maybe it can help other.

当我正在寻找同样的问题时,我会落在这里,也许它可以帮助其他人。

I think the real solution is:

我认为真正的解决方案是:

cat *.log | grep -H somethingtosearch

#8


0  

It is similar to BVB Media's answer.

它类似于BVB Media的答案。

grep -rnw 'blablabla' `pwd`

It works fine on my ubuntu bash.

它在我的ubuntu bash上工作正常。

#9


-1  

Use:

使用:

grep somethingtosearch *.log

and the filenames will be printed out along with the matches.

并且文件名将与匹配一起打印出来。

#1


10  

Assuming you have two log-files in:

假设您有两个日志文件:

  • C:/temp/my.log
  • C:/temp/my.log
  • C:/temp/alsoMy.log
  • C:/temp/alsoMy.log

cd to C: and use:

cd到C:并使用:

grep -r somethingtosearch temp/*.log

It will give you a list like:

它将为您提供如下列表:

temp/my.log:somethingtosearch
temp/alsoMy.log:somethingtosearch1
temp/alsoMy.log:somethingtosearch2

#2


17  

Have you tried using the -l flag?

你尝试过使用-l标志吗?

grep -l somethingtosearch

This will return just the paths and file names where the search was found, not the whole lines where the match was made.

这将仅返回找到搜索的路径和文件名,而不是返回匹配的整行。

Use with -r flag for recursion.

与-r标志一起使用以进行递归。

#3


5  

If you want to see the full paths, I would recommend to cd to the top directory (of your drive if using windows)

如果你想看到完整的路径,我建议cd到*目录(如果使用Windows,你的驱动器)

cd C:\
grep -r somethingtosearch C:\Users\Ozzesh\temp

Or on Linux:

或者在Linux上:

cd /
grep -r somethingtosearch ~/temp

if you really resist on your file name filtering (*.log) AND you want recursive (files are not all in the same directory), combining find and grep is the most flexible way:

如果你真的抵制你的文件名过滤(* .log)并且你想要递归(文件不是全部​​在同一目录中),组合find和grep是最灵活的方式:

cd /
find ~/temp -iname '*.log' -type f -exec grep somethingtosearch '{}' \;

#4


2  

Command:

命令:

grep -rl --include="*.js" "searchString" ${PWD}

Returned output:

退货产量:

/root/test/bas.js

#5


1  

for me
grep -b "searchsomething" *.log worked as I wanted

对我来说grep -b“searchsomething”* .log按照我的意愿工作

#6


1  

The easiest way to print full paths is replace relative start path with absolute path:

打印完整路径的最简单方法是用绝对路径替换相对起始路径:

grep -r --include="*.sh" "pattern" ${PWD}

#7


1  

I fall here when I was looking exactly for the same problem and maybe it can help other.

当我正在寻找同样的问题时,我会落在这里,也许它可以帮助其他人。

I think the real solution is:

我认为真正的解决方案是:

cat *.log | grep -H somethingtosearch

#8


0  

It is similar to BVB Media's answer.

它类似于BVB Media的答案。

grep -rnw 'blablabla' `pwd`

It works fine on my ubuntu bash.

它在我的ubuntu bash上工作正常。

#9


-1  

Use:

使用:

grep somethingtosearch *.log

and the filenames will be printed out along with the matches.

并且文件名将与匹配一起打印出来。