我要表演
s/please (\S+) this/\1/
在我的外壳中,在一行中的文件上。
我正在寻找grep、sed 和 perl中最短且最容易记住的解决方案。
示例文件example.txt
:
please grep this
please sed this
I am grumpy cat
please perl this
的示例输出[the program] [your options] [your way of writing the regex] example.txt
:
grep
sed
perl
我尝试过的事情,我希望工作,但没有:
grep "please (\S*)" example.txt
grep "please (\\S*)" example.txt
grep -o "please (\S*)" example.txt
grep -o "^please (\\S*)" example.tx
sed -E "s/^please (.*)/\1/" example.txt
sed -E "s/^please (.*)/\1/g" example.txt
sed -E "s/^please (.*)/\1/p" example.txt
sed -rn "s/^please (\w+) /\1/" example.txt
sed -rn "s/^please (\w+) /\1/g" example.txt
sed -rn "s/^please (\S+) /\1/p" example.txt
sed -rn "s/^please (\S+) /\0/" example.txt
sed -rn "s/^please ([a-zA-Z]+) /\\1/p" example.txt
perl -p -e 's/please (\w+) /$1/' example.txt
perl -p -e 's/.*please (\w+) /$1/' example.txt
perl -p -e 's/.*please (\w+) /\1/' example.txt
perl -p -e 's/.*please (\S*).*/\1/' example.txt
perl -p -e 's/.*please (\S*).*/$1/' example.txt
perl -p -e 's/please (\S+)/$1/g' example.txt
perl -p -n -e 's/.*please (\w+) .*/$1/g' example.txt
perl -p -n -e 'print if s/.*please (\w+) .*/$1/g' example.txt
perl -p -n -e 'print if s/.*please (\w+) .*/\1/g' example.txt
perl -n -e 'print if s/please (\w+) /\1/g' example.txt
perl -n -e 's/please (\w+) /\1/g' example.txt
perl -n -e 's/.*please (\w+)/\1/g' example.txt
cat example.txt | perl -ne 's/.*please (\w+) .*/\1/'
perl -n -e 's/please (\w+) /$1/ and print' example.txt
perl -n -e 's/please (\S+) /$1/ and print' example.txt
额外说明:
- 我对使用列的解决方案不感兴趣,例如 using
cut
,或其他解决方案比正则表达式功能更弱的示例问题。关键是要捕获群体。 +
必须支持扩展的正则表达式(例如)。- 每个给出的非正则表达式选项,比如
-p
在 perl 调用中,都应该简要解释一下,以便那些只想在谷歌上快速回答的人。