How do you print lines between two patterns with `sed` and `awk`?
IntermediateConceptText processing: grep, sed, awk, cut, sort, uniq
Answer
sed -n '/^\[mysqld\]/,/^\[/p' my.cnf # inclusive range
awk '/BEGIN CERT/,/END CERT/' bundle.pem
awk '/^\[mysqld\]/{f=1; next} /^\[/{f=0} f' my.cnf # contents only, without markers
Range patterns turn on at the first pattern and off after the second. If the end pattern never appears, they print to the end of the file.
More on Text processing: grep, sed, awk, cut, sort, uniq
What are the most useful `grep` flags?BeginnerWhat is the difference between basic, extended and Perl regular expressions in grep?IntermediateWhat are the core `sed` commands you use in automation?BeginnerWhy does `sed -i` behave differently on macOS and Linux?IntermediateHow do you use variables inside a `sed` expression safely?AdvancedWhat is the structure of an `awk` program?Beginner