我想过滤行,例如使用
grep -v keyword $file1 > $file2
但我希望输出文件具有与输入文件相同的“文件结尾”情况,即如果 file1 包含尾随换行符,则 file2 也应包含尾随换行符,只有这样。
如上所述使用“grep”,即使 file1 不包含尾随换行符,file2 也始终包含尾随换行符。
最佳答案
使用适用于 RT 的 GNU awk:
$ cat with_nl
a
x keyword y
c
$
$ cat without_nl
a
x keyword y
c$
$
$ awk '{ORS=RT} !/keyword/' with_nl
a
c
$ awk '{ORS=RT} !/keyword/' without_nl
a
c$
关于regex - Linux grep/awk : Filter lines in a file, 但在 EOF 处克隆(保留或省略)尾随换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30424919/