简体   繁体   English

插入 '。' 在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./'

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 如何通过使用其他bash默认函数从tail -f创建的缓冲区接收输入时,只需键入'Result'并将其作为grep的regex参数传递给grep

tail -f file.txt | grep -a -e "$(echo Result | sed 's/./&./g')"

This echoes the word Result as input to sed (consider a here-string instead), which replaces each character with itself followed by a . 这回应了Result这个词作为sed输入(考虑一个here-string ),它用自己替换每个字符后跟一个. , and then the output is used as the search expression for grep . ,然后输出用作grep的搜索表达式。 The -e protects you from mishaps if you want to search for -argument with the dots, for example. 例如,如果您想搜索-argument-e可以保护您免受意外事故的影响。 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')"

The awk version: awk版本:

tail -f file.txt | 
awk -v word="Result" '
    BEGIN {gsub(/./, "&.", word); sub(/\.$/, "", word)} 
    $0 ~ word
'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM