Shell – Empty a file without grep subsequently treating it like a binary file

grepshelltext processing

Currently I have netcat piping output to tee which is writing to output.txt with

nc -l -k -p 9100 | tee output.txt

I want to monitor this output, so I'm watching it with tail -f | egrep -i 'regex' via PuTTY so that I only see relevant bits.

Every now and then I want to clear the output file. The issue arises that if I do > output.txt and then again try to tail -f | egrep ... I get no output. If I grep through the file, I get no matches, despite knowing that there should be matches (as cat output.txt spits out the file properly)

mitch@quartz:~$ grep output.txt -e 'regex'
Binary file output.txt matches

While the same command on output.txt before emptying it works fine.

Basically: > makes grep think my file is a binary file and it won't properly search. Is there a better way to clear the file?

Best Answer

If the only problem is that grep treats it as binary, tell grep to search it regardless:

$ head /bin/bash > out
$ echo "test" >> out 
$ grep test out 
Binary file out matches
$ grep -a test out 
test

From man grep:

   -a, --text
          Process  a binary file as if it were text; this is equivalent to
          the --binary-files=text option.
Related Question