Bash 如何同时grep和cut
在同时进行打灰和切割时遇到问题 我有一个文件Bash 如何同时grep和cut,bash,grep,cut,Bash,Grep,Cut,在同时进行打灰和切割时遇到问题 我有一个文件test.txt。 文件中包含以下语法 File: blah.txt Location: /home/john/Documents/play/blah.txt File: testing.txt Location /home/john 我的命令是/delete-r(filename),比如filename是blah.txt 我如何搜索test.txt以查找blah.txt,并将/home/john/Documents/play/blah.txt剪切出
test.txt
。
文件中包含以下语法
File: blah.txt Location: /home/john/Documents/play/blah.txt
File: testing.txt Location /home/john
我的命令是/delete-r(filename)
,比如filename是blah.txt
我如何搜索
test.txt
以查找blah.txt
,并将/home/john/Documents/play/blah.txt
剪切出来,并将其放入变量中始终希望为您的任务调用尽可能少的外部命令
grep -P "^File: blah\.txt Location: .+" test.txt | cut -d: -f3
您可以使用singleawk命令实现所需:
试试这个;)
您的问题令人困惑-是否要将文件路径“/home/john/Documents/play/blah.txt”存储在一个变量中,并且该文本是test.txt中的一行?是的,我想将位置存储在一个变量中。@Johnathon:您应该通过单击左侧的勾号来接受答案。您可以使用
grep-P删除cat“^File:blah\.txt位置:.+”test.txt | cut-d:-f3
您很少需要cat
命令。回答很好。一个命令没有管道,而且比cut/grep组合更容易维护。
awk '/^File: blah.txt/ { print $4 }' test.txt
filename=$(grep 'blah.txt' test.txt | grep -oP 'Location:.*' | grep -oP '[^ ]+$')
./delete $filename