Bash脚本。在数字范围前插入空行

时间:2011-10-10 14:03:30

标签: linux bash sed

我需要一个bash脚本来格式化文本文件。它必须在一系列数字之前添加一个空行。 类似于下图中的示例。我在尝试:

sed '1{x;d};$H;/^([1-9][0-9]{0,2}|[1-4][0-9]{3}|5000)$/{x;s/^/\n/;b};x '/home/name/zxc' > vvvv

但它没有运行。

提前致谢。

source screenshot

target screenshot

1 个答案:

答案 0 :(得分:1)

这对您有什么用处:

 sed '/^[0123456789]\+$/{x;p;x;}' < input.txt

来自http://sed.sourceforge.net/sed1line.txt

 # double space a file
 sed G

 # double space a file which already has blank lines in it. Output file
 # should contain no more than one blank line between lines of text.
 sed '/^$/d;G'

 # triple space a file
 sed 'G;G'

 # undo double-spacing (assumes even-numbered lines are always blank)
 sed 'n;d'

 # insert a blank line above every line which matches "regex"
 sed '/regex/{x;p;x;}'

 # insert a blank line below every line which matches "regex"
 sed '/regex/G'

 # insert a blank line above and below every line which matches "regex"
 sed '/regex/{x;p;x;G;}'