使用 GNU awk:
awk -v RS='"' 'NR % 2 == 0 { gsub(/\n/, ""); } { printf("%s%s", $0, RT) }' filename
这是最直接的方法。使用"作为记录分隔符,
NR % 2 == 0 { # in every other record (those inside quotes)
gsub(/\n/, "") # remove the newlines
}
{
printf("%s%s", $0, RT) # then print the line terminated by the same thing
# as in the input (to avoid an extra quote at the
# end of the output)
}
RT 是一个 GNU 扩展,这就是为什么这需要 gawk。
使用 sed
用 sed 做这件事的难点在于引号之间可能有两个换行符,比如
line2"test
123
2015"
这使得在条件之后仅获取一行变得脆弱。因此:
sed '/^[^"]*"[^"]*$/ { :a /\n.*"/! { N; ba; }; s/\n//g; }' filename
即:
/^[^"]*"[^"]*$/ { # When a line contains only one quote
:a # jump label for looping
/\n.*"/! { # until there appears another quote
N # fetch more lines
ba
}
s/\n//g # once done, remove the newlines.
}
作为单行程序,这需要 GNU sed,因为 BSD sed 对分支指令的格式很挑剔。但是,应该可以将代码的扩展形式放入一个文件中,例如 foo.sed,然后使用 BSD sed 运行 sed -f foo.sed filename。
请注意,此代码假定在开始引号之后,包含引号的下一行仅包含该引号。如果需要,解决该问题的方法是
sed ':a h; s/[^"]//g; s/""//g; /"/ { x; N; s/\n//; ba }; x' filename
...但这可以说超出了 sed 应该合理完成的事情的范围。它的工作原理是这样的:
:a # jump label for looping
h # make a copy of the line
s/[^"]//g # isolate quotes
s/""//g # remove pairs of quotes
/"/ { # if there is a quote left (number of quotes is odd)
x # swap the unedited text back into the pattern space
N # fetch a new line
s/\n// # remove the newline between them
ba # loop
}
x # swap the text back in before printing.
使用非 GNU awk
每行有几个引号的情况在 awk 中比在 sed 中更容易处理。上面的 GNU awk 代码是隐式执行的;对于非 GNU awk,它需要做更多的事情(但不是很严重):
awk -F '"' '{ n = 0; line = ""; do { n += NF != 0 ? NF - 1 : 0; line = line $0 } while(n % 2 == 1 && getline == 1) print line }' filename
主要技巧是使用" 作为字段分隔符,以便字段数告诉我们行中有多少引号。那么:
{
# reset state
n = 0 # n is the number of quotes we have
# seen so far
line = "" # line is where we assemble the output
# line
do {
n += NF != 0 ? NF - 1 : 0; # add the number of quotes in the line
# (special handling for empty lines
# where NF == 0)
line = line $0 # append the line to the output
} while(n % 2 == 1 && getline == 1) # while the number of quotes is odd
# and there's more input, get new lines
# and loop
print line # once done, print the combined result.
}