替换为"\ n"与awk中的换行符 [英] Replace "\n" with newline in awk
问题描述
我正在跟踪日志,并且它们输出\n
而不是换行符.
I'm tailing logs and they output \n
instead of newlines.
我以为可以将tail
输送到awk
并进行简单的替换,但是我似乎无法在正则表达式中转义换行符.在这里,我演示的是cat
而不是tail
的问题:
I thought I'd pipe the tail
to awk
and do a simple replace, however I cannot seem to escape the newline in the regex. Here I'm demonstrating my problem with cat
instead of tail
:
test.txt
:
John\nDoe
Sara\nConnor
cat test.txt | awk -F'\\n' '{ print $1 "\n" $2 }'
所需的输出:
John
Doe
Sara
Connor
实际输出:
John\nDoe
Sara\nConnor
因此,看起来\\n
与test.txt中名字和姓氏之间的\n
不匹配,而是每行末尾的换行符.
So it looks like \\n
does not match the \n
between the first and last names in test.txt but instead the newline at the end of each line.
看起来\\n
不是在终端中转义的正确方法吗?这种转义方式在例如崇高文字:
It looks like \\n
is not the right way of escaping in the terminal right? This way of escaping works fine in e.g. Sublime Text:
推荐答案
如何?
$ cat file
John\nDoe
Sara\nConnor
$ awk '{gsub(/\\n/,"\n")}1' file
John
Doe
Sara
Connor
这篇关于替换为"\ n"与awk中的换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!