Skip to main content
2 of 2
added 850 characters in body
Stéphane Chazelas
  • 585.1k
  • 96
  • 1.1k
  • 1.7k

You can always add your new entry with a x. newentry syntax and renumber everything afterwards with something like:

awk -F . -v OFS=. '{if (NF) $1 = ++n; else n = 0; print}'
  • -F .: sets the field separator to .1
  • -v OFS=.: same for the output field separator (-F . is short for -v FS=.).
  • {...}: no condition so the code inside {...} is run for each line
  • if (NF), if the number of fields is greater than 0. With FS being ., that means if the current line contains at least one .. We could also make it if (length) to check for non-empty lines.
  • $1 = ++n: set the first field to an incremented n (initially 0, then 1, then 2...).
  • else n = 0: else (when NF == 0) reset n to 0.
  • print: print the (possibly modified) line.

1The syntax is -F <extended-regular-expression> but when <extended-regular-expression> is a single character, that is not taken as a regular expression (where . means any character) but as that character instead.

Stéphane Chazelas
  • 585.1k
  • 96
  • 1.1k
  • 1.7k