Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

bash - Replace " " with newline in awk

I'm tailing logs and they output instead of newlines.

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
Doe
Sara
Connor
cat test.txt | awk -F'\n' '{ print $1 "
" $2 }'

Desired output:

John
Doe
Sara
Connor

Actual output:

John
Doe

Sara
Connor

So it looks like \n does not match the between the first and last names in test.txt but instead the newline at the end of each line.

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:

regex working in ST3

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

How about this?

$ cat file
John
Doe
Sara
Connor

$ awk '{gsub(/\n/,"
")}1' file
John
Doe
Sara
Connor

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...