如果某行以某个字符开头,请删除上一个换行符

时间:2014-10-04 00:02:29

标签: regex linux bash sed

我有一系列来自故事的线条:

Little Sue went
out to play, and
found herself a log.
The log was
heavy.

我想解开文字。如果一行以小写字母开头,则应删除上一个换行符,并替换为空格,结果如下:

Little Sue went out to play, and found herself a log.
The log was heavy.

我发现了一些sed在换行符之间进行模式匹配的示例,但它们总是用于在模式匹配后删除换行符。我已经尝试在sed中构建一些命令,但它的工作不正常。他们似乎只在每一条线上行动。我认为这是因为sed用一对线完成打印,然后在消耗下一行之前抛弃它们。

sed 'N;/\n[a-z]/s/\n/ /;P;D' wrapped.txt
  Little Sue went out to play, and
  found herself a log.
  The log was heavy.
sed 'N;/\n[a-z]/s/\n\([a-z]\)/ \1/' wrapped.txt
  Little Sue went out to play, and
  found herself a log.
  The log was
  heavy.

我的Fedora 20机器上有GNU sed 4.2.2。有谁知道sed是否能够满足我的需求?或者,如果sed不会这样做,那么Linux盒子上常用的任何程序或语言都可以。

1 个答案:

答案 0 :(得分:1)

这可以通过Perl轻松完成,

$ perl -00pe 's/\n(?=[a-z])/ /g' file
Little Sue went out to play, and found herself a log.
The log was heavy.