1

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

Alexander McFarlane
  • 10,643
  • 9
  • 59
  • 100
  • I'm a bit confused; so you want to do `tail -f file.txt | grep 'Result'` and have it print out results for `R.e.s.u.l.t.`? – Explosion Pills Jun 23 '15 at 23:48
  • yes because I don't want to have to insert dots between every letter as it's a bit tedious when the matching word / sentence becomes long – Alexander McFarlane Jun 23 '15 at 23:50
  • Why do you want to do this? It seems a curious way of doing searching. Is it something to do with UTF-16 data? The `-a` option is _equivalent to `--binary-files=text`_, and `-binary-files=TYPE` is _assume that binary files are TYPE; TYPE is 'binary', 'text', or 'without-match'_, so it would look for ASCII characters in a UTF16 file, where the other bytes would be null or zero bytes. – Jonathan Leffler Jun 23 '15 at 23:58

2 Answers2

2
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 ., 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')"
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
1

The awk version:

tail -f file.txt | 
awk -v word="Result" '
    BEGIN {gsub(/./, "&.", word); sub(/\.$/, "", word)} 
    $0 ~ word
'
glenn jackman
  • 238,783
  • 38
  • 220
  • 352