插 '。'在grep正则表达式中的所有字符之间,并在管道grep中使用 [英] insert '.' between all characters in grep regex and use in piped grep
问题描述
我想在给定的输入字符串中的每个字符之间插入'。'
,然后将其用作管道中的参数
I want to insert '.'
between every character in a given input string and then use it as an argument in a pipe
我正在执行以下任一操作:
I am doing one of the following:
tail -f file.txt | grep -a 'R.e.s.u.l.t.'
tail -f file.txt | awk '/R.e.s.u.l.t./'
如何输入'Result'
,并且当
从由创建的缓冲区接收输入时,将它作为正则表达式参数传递给
通过使用额外的 grep
尾部-f bash
默认函数
How can I just type 'Result'
and pass it as the regex argument to grep
when
receiving input from a buffer created by tail -f
by using additional bash
default functions
推荐答案
tail -f file.txt | grep -a -e "$(echo Result | sed 's/./&./g')"
这反映了结果
作为 sed
的输入(考虑 here-string ),它用自身替换每个字符,后跟一个。
,然后将输出用作 grep $的搜索表达式C $ C>。例如,如果你想用点来搜索
-argument
,那么 -e
可以防止意外事故发生。如果字符串在变量中,那么您也可以使用双引号:
This echoes the word Result
as input to sed
(consider a here-string instead), which replaces each character with itself followed by a .
, and then the output is used as the search expression for grep
. The -e
protects you from mishaps if you want to search for -argument
with the dots, for example. If the string is in a variable, then you'd use double quotes around that, too:
result="Several Words"
tail -f file.txt | grep -a -e "$(echo "$result" | sed 's/./&./g')"
这篇关于插 '。'在grep正则表达式中的所有字符之间,并在管道grep中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!