Reputation: 181
I want to grep 'run' numbers in some files e.g.
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.
Upvotes: 0
Views: 88
Reputation: 118
I think the problem are the curly brackets around *, just try egrep -o run[0-9]*
.
If you don't want to match "run", try egrep -o run[0-9]+
.
Upvotes: 1
Reputation: 5249
Try this:
for f in $(find . -name testlog)
do
echo $(basename $(dirname $f))
done
Upvotes: 0
Reputation: 10423
You need to replace ${pwd} with $f and should quote the regular expression.
I would use sed and avoid the for loop (but you can also use grep -o, if you use the pattern correctly, i.e. without braces)
find . -name testlog -print | sed -n 's/.*\/\(run[0-9]*\)\/testlog/\1/p'
or
find . -name testlog -print | grep -o 'run[0-9]+'
Upvotes: 0