bash 如何使用grep只获取没有路径的文件名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24806674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 10:53:28  来源:igfitidea点击:

How to get only filenames without Path by using grep

linuxbashgrep

提问by Thomas Lang

I have got the following Problem.

我有以下问题。

I′m doing a grep like:

我正在做一个 grep 像:

$command = grep -r -i --include=*.cfg 'host{' /omd/sites/mesh/etc/icinga/conf.d/objects

$command = grep -r -i --include=*.cfg 'host{' /omd/sites/mesh/etc/icinga/conf.d/objects

I got the following output:

我得到以下输出:

/omd/sites/mesh/etc/icinga/conf.d/objects/testsystem/test1.cfg:define host{
/omd/sites/mesh/etc/icinga/conf.d/objects/testsystem/test2.cfg:define host{
/omd/sites/mesh/etc/icinga/conf.d/objects/testsystem/test3.cfg:define host{
...

for all *.cfg files.

对于所有 *.cfg 文件。

With exec($command,$array)

exec($command,$array)

I passed the result in an array.

我在一个数组中传递了结果。

Is it possible to get only the filenames as result of the grep-command.

是否可以仅获取文件名作为 grep 命令的结果。

I have tried the following:

我尝试了以下方法:

$Command=    grep -l -H -r -i --include=*.cfg 'host{' /omd/sites/mesh/etc/icinga/conf.d/objects

but I got the same result.

但我得到了同样的结果。

I know that on the forum a similar topic exists.(How can I use grep to show just filenames (no in-line matches) on linux?), but the solution doesn′t work.

我知道在论坛上存在类似的主题。(如何在 linux 上使用 grep 仅显示文件名(无内联匹配)?),但该解决方案不起作用。

With "exec($Command,$result_array)" I try to get an array with the results. The mentioned solutions works all, but I can′t get an resultarray with exec().

使用“exec($Command,$result_array)”,我尝试获取一个包含结果的数组。提到的解决方案都有效,但我无法使用 exec() 获得 resultarray。

Can anyone help me?

谁能帮我?

回答by olivecoder

Yet another simpler solution:

另一个更简单的解决方案:

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

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

或者xargs,如果您不使用旧版本的 GNU coreutils,则可以避免并使用子外壳:

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

basenameis the bash straightforward solution to get a file name without path. You may also be interested in dirnameto get the path only.

basename是获取没有路径的文件名的 bash 直接解决方案。您可能还对dirname仅获取路径感兴趣。

GNU Coreutils basename documentation

GNU Coreutils 基本名称文档

回答by Sylvain Leroux

Is it possible to get only the filenames as result of the grepcommand.

是否可以仅获取文件名作为grep命令的结果。

With grepyou need the -loption to display only file names.

随着grep你所需要的-l,只显示文件名的选项。

Using find ... -execdir grep ... \{} +you might prevent displaying the full path of the file (is this what you need?)

使用find ... -execdir grep ... \{} +您可能会阻止显示文件的完整路径(这是您需要的吗?)

find /omd/sites/mesh/etc/icinga/conf.d/objects -name '*.cfg' \
     -execdir grep -r -i -l 'host{' \{} +


In addition, concerning the second part of your question, to read the result of a command into an array, you have to use the syntax: IFS=$'\n' MYVAR=( $(cmd ...) )

此外,关于您问题的第二部分,要将命令的结果读入数组,您必须使用以下语法:IFS=$'\n' MYVAR=( $(cmd ...) )

In that particular case (I formatted as multiline statement in order to clearly show the structure of that expression -- of course you could write as a "one-liner"):

在那种特殊情况下(我格式化为多行语句以清楚地显示该表达式的结构——当然你可以写成“单行”):

IFS=$'\n' MYVAR=(
    $(
        find objects -name '*.cfg' \
                     -execdir grep -r -i -l 'host{' \{} +
    )
)

You have then access to the result in the array MYVARas usual. While I while I was testing (3 matches in that particular case):

然后您可以MYVAR像往常一样访问数组中的结果。当我在测试时(在这种特殊情况下有 3 场比赛):

sh$ echo ${#MYVAR[@]}
3
sh$ echo ${MYVAR[0]}
./x y.cfg
sh$ echo ${MYVAR[1]}
./d.cfg
sh$ echo ${MYVAR[2]}
./e.cfg
# ...

回答by djhaskin987

This should work:

这应该有效:

grep -r -i --include=*.cfg 'host{' /omd/sites/mesh/etc/icinga/conf.d/objects | \
    awk '{print }' | sed -e 's|[^/]*/||g' -e 's|:define$||'

The awkportion finds the first field in it and the sedcommand trims off the path and the :define.

awk部分找到其中的第一个字段,sed命令修剪掉路径和:define.