sed替换文本

时间:2015-05-12 11:28:03

标签: regex linux bash text sed

我有这样的文字:

0
4.496
4.496
Plain text.
7.186
Plain text
10.949
Plain text
12.988
Plain text
16.11
Plain text
17.569
Plain text
ESP:
Plain text

我正在尝试进行sed替换,因为我需要在数字后对齐,例如:

0
4.496
4.496 Plain text.
7.186 Plain text
10.949 Plain text
12.988 Plain text
16.11 Plain text
17.569 Plain text ESP:Plain text

但我尝试使用sed的不同命令组合,但我无法保留匹配模式的一部分

sed -r 's/\([0-9]+\.[0-9]*\)\s*/\1/g'

我正在尝试删除数字后的所有\n并对齐文本,但它不起作用。我也需要将文本与文本对齐。

我也尝试了这个:

sed -r 's/\n*//g'

但没有结果。

谢谢

4 个答案:

答案 0 :(得分:2)

这有点棘手。你的方法不起作用,因为sed以一种基于行的方式运行(它读取一行,运行代码,读取另一行,运行代码等等),所以除非你这样做,否则它不会看到换行符特别的东西。我们必须完全覆盖sed的正常控制流程。

使用GNU sed:

sed ':a $!{ N; /\n[0-9]/ { P; s/.*\n//; }; s/\n/ /; ba }' filename

其工作原理如下:

:a                # Jump label for looping (we'll build our own loop, with
                  # black jack and...nevermind)
$! {              # Unless the end of the input was reached:
  N               # fetch another line, append it to what we already have
  /\n[0-9]/ {     # if the new line begins with a number (if the pattern space
                  # contains a newline followed by a number)
    P             # print everything up to the newline
    s/.*\n//      # remove everything before the newline
  }
  s/\n/ /         # remove the newline if it is still there
  ba              # go to a (loop)
}
                  # After the end of the input we drop off here and implicitly
                  # print the last line.

代码可以适用于BSD sed(在* BSD和Mac OS X上找到),但BSD sed对标签和跳转指令有点挑剔。我相信

sed -e ':a' -e '$!{ N; /\n[0-9]/ { P; s/.*\n//; }; s/\n/ /; ba' -e '}' filename

应该有用。

答案 1 :(得分:2)

这个gnu-awk命令也可以处理这个:

awk -v RS= 'BEGIN{ FPAT="(^|\n)[0-9]+(\\.[0-9]+)?(\n[^0-9][^\n]*)*" }
   {for (i=1; i<=NF; i++) {f=$i; sub(/^\n/, "", f); gsub(/\n/, " ", f); print f}}' file
0
4.496
4.496 Plain text.
7.186 Plain text
10.949 Plain text
12.988 Plain text
16.11 Plain text
17.569 Plain text ESP: Plain text

答案 2 :(得分:1)

-n
  • '1h;1!H;$!b;x:仅按需打印
  • s/\n\([^0-9]\)/ \1/gp将整个文件加载到缓冲区中直到结束(整个文件在工作缓冲区的末尾)
  • <p>Some text random 1</p> <p>Some text random 2</p> <p>Some text random 3</p> <div><img src=""></div> <p>Some text random 4</p> <p>Some text random 5</p> :用空格字符替换所有新行后跟非数字字符并打印结果。

答案 3 :(得分:1)

这可能适合你(GNU sed):

sed ':a;N;s/\n\([[:alpha:]]\)/ \1/;ta;P;D' file

一次处理两行。如果第二行以字母字符开头,则删除前面的换行符并追加另一行并重复。如果第二行不以字母字符开头,则打印然后删除第一行及其换行符。第二行现在成为第一行,并重复该过程。