简体   繁体   English

bash grep文件目录

[英]bash grep file directories

I want to grep 'run' numbers in some files eg 我想在某些文件中grep'运行'数字

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

I have the following code: 我有以下代码:

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

I want my code to output: 我希望我的代码输出:

run3
run14
run28

However I am not getting any output. 但是我没有得到任何输出。

I think the problem are the curly brackets around *, just try egrep -o run[0-9]* . 我认为问题在于*周围的花括号,只需尝试egrep -o run[0-9]*

If you don't want to match "run", try egrep -o run[0-9]+ . 如果您不想匹配“ run”,请尝试egrep -o run[0-9]+

You need to replace ${pwd} with $f and should quote the regular expression. 您需要用$ f替换$ {pwd}并应引用正则表达式。

I would use sed and avoid the for loop (but you can also use grep -o, if you use the pattern correctly, ie without braces) 我会使用sed并避免for循环(但如果正确使用模式(即不使用大括号),也可以使用grep -o)

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

or 要么

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

Try this: 尝试这个:

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

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

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