Extract lines between 2 tokens in a text file using bash

You can extract it, including the tokens with sed. Then use head and tail to strip the tokens off.

... | sed -n "/this is token 1/,/this is token 2/p" | head -n-1 | tail -n+2

No need for head and tail or grep or to read the file multiple times:

sed -n '/<!-- this is token 1 -->/{:a;n;/<!-- this is token 2 -->/b;p;ba}' inputfile

Explanation:

  • -n - don't do an implicit print
  • /<!-- this is token 1 -->/{ - if the starting marker is found, then
    • :a - label "a"
      • n - read the next line
      • /<!-- this is token 2 -->/q - if it's the ending marker, quit
      • p - otherwise, print the line
    • ba - branch to label "a"
  • } end if

Tags:

Bash