使用 vi 编辑器在每个 X 行插入新文本

Insert new text at every X line using vi editor

我目前有一个 yaml 文件(这个文件很大,格式如下):

- created_at: 2021-01-18 15:11:44
  created_by: joe
  hostname: sfo
  ip: 1.1.1.1
  opersys: tmsh
  platform: f5
  role: lb
  type: loadbalancer
- created_at: 2021-01-18 15:11:44
  created_by: joe
  hostname: sjc
  ip: 2.2.2.2
  opersys: tmsh
  platform: f5
  role: lb
  type: loadbalancer

我想在每 8 行插入一个新文本 (serial_num: xxxxx),因此它看起来像这样:

- created_at: 2021-01-18 15:11:44
  created_by: joe
  hostname: sfo
  ip: 1.1.1.1
  opersys: tmsh
  platform: f5
  role: lb
  type: loadbalancer
  serial_num: xxxxx
- created_at: 2021-01-18 15:11:44
  created_by: joe
  hostname: sjc
  ip: 2.2.2.2
  opersys: tmsh
  platform: f5
  role: lb
  type: loadbalancer
  serial_num: xxxxx

纯Vim,:help :global:help :put

" after every line matching 'type:'
:g/type:/put='  serial_num: xxxxx'

" before every line matching 'type:'
:g/type:/put!='  serial_num: xxxxx'

纯Vim,:help :global:help :normal

" after every line matching 'type:'
:g/type:/normal oserial_num: xxxxx

" before every line matching 'type:'
:g/type:/normal Oserial_num: xxxxx

使用 GNU Sed

sed '/type: /a\  serial_num: xxxxx' your_file