How to grep and cut at the same time
Having trouble with grepping and cutting at the same time
I have a file test.t开发者_如何学运维xt
.
Inside the file is this syntax
File: blah.txt Location: /home/john/Documents/play/blah.txt
File: testing.txt Location /home/john
My command is ./delete -r (filename)
, say filename is blah.txt
.
How would i search test.txt
for blah.txt
and cut the /home/john/Documents/play/blah.txt
out and put it in a variable
grep -P "^File: blah\.txt Location: .+" test.txt | cut -d: -f3
Prefer always to involse as less as possible external command for your task.
You can achive what you want using single awk command:
awk '/^File: blah.txt/ { print $4 }' test.txt
Try this one ;)
filename=$(grep 'blah.txt' test.txt | grep -oP 'Location:.*' | grep -oP '[^ ]+$')
./delete $filename
精彩评论