linux - bash 脚本。在数字范围前插入一个空行

标签 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

最佳答案

这对你有何作用:

 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;}'

关于linux - bash 脚本。在数字范围前插入一个空行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7713947/

相关文章:

php - 构建C++共享库,.so被激活,等待PHP网页调用

linux - 如何在 ubuntu 中使用命令行在 Jenkins 中安装插件?

regex - Bash for 循环打印出意外的输出,这是为什么?

sed - 在 find 命令中获取当前目录并在 sed 中使用 - 一行

regex - 使用sed从文件中提取单词

java - 像 Windows 一样的 Linux 上的想法键盘映射

linux - SetHandleInformation() Linux 等效项

android - linux shell - 无法将字符串与 : adb shell getprop ro. product.brand 进行比较

bash - vim:如果为空,则将第二列空白替换为同一列中的值

Bash IF 语句 : string comparison of command output always false