如何通过使用grep获得没有路径的文件名 [英] How to get only filenames without Path by using grep

查看:232
本文介绍了如何通过使用grep获得没有路径的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在做一个类似于grep的命令:

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



我得到了以下输出:

  /omd/sites/mesh/etc/icinga/conf.d/objects/testsystem/test1.cfg :定义主机{
/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 {
...

所有* .cfg文件。



使用 exec($ command,$ array)



我将结果传递给一个数组。



是否有可能仅获取grep-command的结果文件名。



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

但我得到了同样的结果。



我知道在论坛上存在类似的话题(

使用exec($ Command,$ result_array)我尝试获得一个包含结果的数组。
上面提到的解决方案都可以工作,但是我不能用exec()获得结果阵列。

任何人都可以帮助我吗?

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

  grep -l无论你想要什么| xargs -L 1 basename 

basename是直接获取文件名而不包含路径的解决方案


I have got the following Problem.

I´m doing a grep like:

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

With exec($command,$array)

I passed the result in an array.

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

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.

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

Can anyone help me?

解决方案

Yet another simpler solution:

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

basename is the straight solution to get a file name without path

这篇关于如何通过使用grep获得没有路径的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆