Print lines between 2 words using sed & awk

πŸ“…οΈ Published: July 24, 2020  β€’ πŸ•£ 1 min read

You may arrive in a situation where you may want to β€œextract” out text between two words. For example to view the latest changelog (where x.x.x is the latest version) in a CHANGELOG.md file.

Using sed

sed -n -e '/x.x.x/,/0.1.0/ p' CHANGELOG.md | sed -e '1d;$d'

sed -e '1d;$d' removes the first & last line.

Using awk

awk '/x.x.x/,/0.1.0/' CHANGELOG.md | awk 'NR>2 {print last} {last=$0}'

awk 'NR>2 {print last} {last=$0}' removes the first & last line.

NOTE: NR means which Line number is being processed

Resources

Tired of fucking up with git everyday?

ugit helps you undo git commands with ease. Undo from 20+ git scenarios

ugit cli demo screen