如何通过使用grep获得没有路径的文件名 [英] How to get only filenames without Path by using 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
但我得到了同样的结果。
我知道在论坛上存在类似的话题( 任何人都可以帮助我吗? 另一个更简单的解决方案: 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: for all *.cfg files. With 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: 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: basename is the straight solution to get a file name without path 这篇关于如何通过使用grep获得没有路径的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
使用exec($ Command,$ result_array)我尝试获得一个包含结果的数组。
上面提到的解决方案都可以工作,但是我不能用exec()获得结果阵列。
grep -l无论你想要什么| xargs -L 1 basename
/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{
...
exec($command,$array)
$Command= grep -l -H -r -i --include=*.cfg 'host{' /omd/sites/mesh/etc/icinga/conf.d/objects
grep -l whatever-you-want | xargs -L 1 basename