使用 bash / linux 在特定行之后创建一个空行

creating a blank line after some specific line using bash / linux

我需要在第 45 行之后使用 sed 添加一个额外的空行

例如:

44 some text one
45 some text two
46 some text three
47 some text four

结果:

44 some text one
45 some text two
46 
47 some text three
48 some text four

我试过

sed '45G' myfile.txt

但似乎不起作用,它会在屏幕上打印文件的内容,但不会在第 45 行

之后添加任何 space

最小化使用 CentOS 7

你可以这样做:

sed $'45 a \n' file.txt
  • $'' 启动 C 风格引用,在某些 sed 使用 \n

  • 时可能需要
  • 45 a \n 在第 45 行

  • 之后 (a) 添加换行符 (\n)

sed 用于在单行上进行简单替换,仅此而已。对于其他任何事情,只需使用 awk:

awk '{print} NR==45{print ""}' file

这将适用于任何 UNIX 机器上的任何 awk。