清空文件而不使用 grep 随后将其视为二进制文件

Mit*_*tch 7 shell grep text-processing

目前我有netcat管道输出tee,正在写入 output.txt

nc -l -k -p 9100 | tee output.txt
Run Code Online (Sandbox Code Playgroud)

我想监视这个输出,所以我tail -f | egrep -i 'regex'通过 PuTTY观看它,这样我只能看到相关的位。

我时不时地想清除输出文件。问题出现了,如果我这样做> output.txt然后再次尝试tail -f | egrep ...我没有输出。如果我对文件进行 grep,尽管知道应该有匹配项(因为cat output.txt正确地吐出文件),但我没有得到匹配项

mitch@quartz:~$ grep output.txt -e 'regex'
Binary file output.txt matches
Run Code Online (Sandbox Code Playgroud)

虽然清空之前output.txt 上的相同命令工作正常。

基本上:>grep我认为我的文件是一个二进制文件,它不会正确搜索。有没有更好的方法来清除文件?

ter*_*don 5

如果唯一的问题是grep将其视为二进制文件,请告诉grep搜索它,无论如何:

$ head /bin/bash > out
$ echo "test" >> out 
$ grep test out 
Binary file out matches
$ grep -a test out 
test
Run Code Online (Sandbox Code Playgroud)

来自man grep

   -a, --text
          Process  a binary file as if it were text; this is equivalent to
          the --binary-files=text option.
Run Code Online (Sandbox Code Playgroud)