如何去除双引号中的新线路? [英] How to remove new lines within double quotes?
问题描述
我怎么可以删除里面的新行从一个文件?
How can I remove new line inside the "
from a file?
例如:
"one",
"three
four",
"seven"
所以我想删除 \\ n
之间的三
和四
。我应该使用常规的前pression,或者我要读这是每个字符的文件与程序?
So I want to remove the \n
between the three
and four
. Should I use regular expression, or I have to read that's file per character with program?
推荐答案
要处理特别是那些在双引号字符串换行符,离开那些单独是他们之外,使用GNU AWK(为 RT
)
To handle specifically those newlines that are in doubly-quoted strings and leave those alone that are outside them, using GNU awk (for RT
):
gawk -v RS='"' 'NR % 2 == 0 { gsub(/\n/, "") } { printf("%s%s", $0, RT) }' file
这工作沿着字符和删除换行符隔挡,随着包含文件
This works by splitting the file along "
characters and removing newlines in every other block. With a file containing
"one",
"three
four",
12,
"seven"
这会给结果
"one",
"threefour",
12,
"seven"
请注意,它不处理转义序列。如果输入的数据字符串可以包含 \\
,如他说:\\这是一个直接引用\\。
,那么它将无法获得理想的工作。
Note that it does not handle escape sequences. If strings in the input data can contain \"
, such as "He said: \"this is a direct quote.\""
, then it will not work as desired.
这篇关于如何去除双引号中的新线路?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!