简体   繁体   English

如何使用 grep 搜索文件路径

[英]How to search for file paths using grep

I am trying to get all of the file paths that are listed in a certain file and store them in an array for further use我正在尝试获取某个文件中列出的所有文件路径并将它们存储在一个数组中以供进一步使用

The file paths within the files look like this:文件中的文件路径如下所示:

include /app/...;

or或者

#include /app/...; #comment

But the the file also has other file paths so I want to find just the ones that start with include.但是该文件还有其他文件路径,所以我只想找到以包含开头的文件。

I tried grep 'include /[^"]*' <fileToSearch> but it is missing some files and doesn't capture them我试过grep 'include /[^"]*' <fileToSearch>但它缺少一些文件并且没有捕获它们

Any help will be appreciated.任何帮助将不胜感激。

You can try this regex:你可以试试这个正则表达式:

grep 'include\s+/+[\w\\\-/.]*' <fileToSearch>

Explanation: this regex match all pathes after the keyword "include" and starting by a / .说明:此正则表达式匹配关键字“include”之后并以/开头的所有路径。 The keyword and the path can be separated by any number of whitespace character.关键字和路径可以用任意数量的空白字符分隔。

grep looks for any line that contains the string and return whole line. grep 查找包含该字符串的任何行并返回整行。

grep "^include." or grep "^#include."grep "^#include."

If you want to get only path (without include statement), you should use sed, awk...如果您只想获取路径(没有包含语句),您应该使用 sed、awk...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM