问题
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. Assplit()
returns how many pieces based on"
as delimiter, at least we get a value of1
.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. Ifval
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