Strip new line characters within a quoted string in unix

落爺英雄遲暮 提交于 2019-12-11 09:27:57

问题


I have a text file that goes like this:

abc 123 xyz
"abc
123" xyz

I want to replace new lines with a space (' ') if the new line occurs within a quoted string. So I want output:

abc 123 xyz
abc 123 xyz

Is there a way to write a program in Unix for this?


回答1:


You can print a new line or just a space depending on how many " how got so far. This way, new line will just be printed if we are closing quotes.

$ awk '{n=split($0,a,"\""); val+=(n-1); gsub("\"",""); printf "%s%s", $0, (val%2?" ":"\n")}' file
abc 123 xyz
abc 123 xyz

Explanation

  • n=split($0,a,"\"") count how many " appear in the current line. As split() returns how many pieces based on " as delimiter, at least we get a value of 1.
  • val+=(n-1) keep track of the balance. -1 to just count the number of quotes, as split returns one more than needed.
  • gsub("\"","") remove double quotes in the string.
  • printf "%s%s", $0, (val%2?" ":"\n") print the line together with a space or a new line. If val is multiple of 2, new line; otherwise, space.

Test

Another example:

$ cat a
abc 123 xyz
"abc
hee
123" xyz
and "this
is not everything"
$ awk '{n=split($0,a,"\""); val+=(n-1); gsub("\"",""); printf "%s%s", $0, (val%2?" ":"\n")}' a
abc 123 xyz
abc hee 123 xyz
and this is not everything


来源:https://stackoverflow.com/questions/25428540/strip-new-line-characters-within-a-quoted-string-in-unix

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!