替换为"\ n"与awk中的换行符 [英] Replace "\n" with newline in awk

查看:355
本文介绍了替换为"\ n"与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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆