bash grep文件目录

时间:2014-07-30 22:38:43

标签: file grep echo

我想在某些文件中grep'运行'数字,例如。

files/run3/testlog
files/run14/testlog
files/run28/testlog

我有以下代码:

for f in $(find . -name testlog)
do
     echo $(pwd) | egrep -o run[0-9]{*}
done

我希望我的代码输出:

run3
run14
run28

但是我没有得到任何输出。

3 个答案:

答案 0 :(得分:1)

我认为问题是围绕*的大括号,只需尝试egrep -o run[0-9]*

如果您不想匹配“run”,请尝试egrep -o run[0-9]+

答案 1 :(得分:0)

你需要用$ f替换$ {pwd}并引用正则表达式。

我会使用sed并避免使用for循环(但如果正确使用模式,也可以使用grep -o,即没有大括号)

find . -name testlog -print | sed -n 's/.*\/\(run[0-9]*\)\/testlog/\1/p'

find . -name testlog -print | grep -o 'run[0-9]+'

答案 2 :(得分:0)

试试这个:

for f in $(find . -name testlog)
do
     echo $(basename $(dirname $f))
done