File bash grep文件目录
我想在一些文件中grep“run”数字,例如File bash grep文件目录,file,grep,echo,File,Grep,Echo,我想在一些文件中grep“run”数字,例如 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 然而,我没有得到任何输出 您需要将${pwd}替换为$f,并引用正则表达式 我会使用sed并避免
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
然而,我没有得到任何输出 您需要将${pwd}替换为$f,并引用正则表达式 我会使用sed并避免for循环(但如果正确使用模式,也可以使用grep-o,即不使用大括号) 或
我认为问题在于*周围的花括号,请尝试
egrep-o run[0-9]*
如果不想匹配“运行”,请尝试以下操作:
for f in $(find . -name testlog)
do
echo $(basename $(dirname $f))
done
(这意味着你真的要把文件名而不是内容变灰)顺便说一句:特别是当你得到“没有输出”时,你应该总是用一个已知的测试模式测试你的正则表达式。我通常使用
echo./run15/testlog | egrep-o'run[0-9]+'
测试阳性案例,使用echo./run/testlog | egrep-o'run[0-9]+'
测试阴性(无编号)案例。
find . -name testlog -print | grep -o 'run[0-9]+'
for f in $(find . -name testlog)
do
echo $(basename $(dirname $f))
done