Insert '.' between all characters in grep regex and usage in piped grep

I want to insert '.'

between each character in a given input line and then use it as an argument in a pipe

I do 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 a regex argument in grep

when receiving input from a buffer created tail -f

using the bash

default additional functions

+1


source to share


2 answers


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

      

This echoes the word Result

as input for sed

(instead is used instead of here), which replaces each character itself that is followed .

, and then the output is used as the search expression for grep

. -e

protects you from failure if you want to search -argument

with dots , for example . If the string is in a variable, then you will also use double quotes:



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

      

+2


source


Awk version:



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

      

+1


source







All Articles