我正在尝试编写一个脚本,该脚本使用 sed 处理文本文件中的行以生成示例文档。我的大部分脚本都在工作,但我遇到了一个边缘情况。考虑以下文件
line-1
line-2, part2
line-3-should-be-a-very-long,
line-3-continued
line-4
Run Code Online (Sandbox Code Playgroud)
问题是有些行(但不是全部)以特殊标记结尾(它恰好是逗号)。该标记表示该行应与后续行连接以产生一个长行。
所以在我的例子中line-3-should-be-a-very-long,应该连接line-3-continued到给我line-3-should-be-a-very-long, line-3-continued(我确实想保留逗号)。第 2 行没有特殊操作,即使它包含一个不在行尾的逗号。
其余的处理是通过管道将一些sed和grep命令组合在一起来完成的,因此 sed 解决方案非常适合。
$ sed '/,$/{N;s/\n//;}' file
line-1
line-2
line-3-should-be-a-very-long, line-3-continued
line-4
Run Code Online (Sandbox Code Playgroud)
如果应删除空格:
$ sed '/,$/{N;s/\n[[:blank:]]*//;}' file
line-1
line-2
line-3-should-be-a-very-long,line-3-continued
line-4
Run Code Online (Sandbox Code Playgroud)
(如果您希望在行之间保留一个空格,请//在代码中替换为/ /)
如果行可以连续多次,如
line-1
line-2
line-3-should-be-a-very-long,
line-3-continued,
line-3-continued-further
line-4
Run Code Online (Sandbox Code Playgroud)
然后,
$ 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
Run Code Online (Sandbox Code Playgroud)
最后一个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
Run Code Online (Sandbox Code Playgroud)