我正在尝试编写一个使用 sed 处理文本文件中的行以生成示例文档的脚本。我已经完成了大部分脚本的工作,但我遇到了一种边缘情况。考虑以下文件
line-1
line-2, part2
line-3-should-be-a-very-long,
line-3-continued
line-4
问题是有些(但不是全部)行以特殊标记结尾(它恰好是逗号)。该标记指示该行应与下一行连接以生成一长行。
因此,在我的示例中line-3-should-be-a-very-long,
应该连接line-3-continued
to 给我line-3-should-be-a-very-long, line-3-continued
(我确实想保留逗号)。第 2 行没有特殊操作,尽管它包含不在行尾的逗号。
其余的处理是通过将一些sed
andgrep
命令通过管道连接在一起来完成的,因此 sed 解决方案非常适合。
答案1
$ sed '/,$/{N;s/\n//;}' file
line-1
line-2
line-3-should-be-a-very-long, line-3-continued
line-4
如果应删除空白:
$ sed '/,$/{N;s/\n[[:blank:]]*//;}' file
line-1
line-2
line-3-should-be-a-very-long,line-3-continued
line-4
(如果您希望行之间保留一个空格,请//
在代码中替换为/ /
)
如果行可以连续多次,如
line-1
line-2
line-3-should-be-a-very-long,
line-3-continued,
line-3-continued-further
line-4
然后,
$ sed '/,$/{:loop;N;s/\n[[:blank:]]*//;/,$/bloop;}' file
line-1
line-2
line-3-should-be-a-very-long,line-3-continued,line-3-continued-further
line-4
最后一个sed
脚本用注释进行了解释:
/,$/{ # if the current line ends with a comma, then...
:loop # define label "loop"
N # append next line from input (a newline will be inserted in-between)
s/\n[[:blank:]]*// # delete that newline and any blanks (tabs or spaces) directly after it
/,$/bloop # if the line now ends with comma, branch to the "loop" label
}
# implicit output of (possibly) modified line at end
答案2
sed '/,$/{N;s/\n[[:blank:]]\+/ /}' file
当您看到以逗号结尾的行时,读入下一行,然后将下一行的换行符和前导空格替换为一个空格。