Perlisms: Print all lines between two regexes

Time for Yet Another Perl One-liner. I occasionally need to yank out some info between two specific line delimiters from a large file. Good ol’ standard grep won’t suffice in this instance, so it is perl to the rescue.

Here is an example on how to print all ‘Section “Device”‘ sections out of an xorg.conf:

perl -ne '(m!^Section "Device"! ... m!EndSection!) && print' /etc/X11/xorg.conf

This regex range will match the beginning of my device section, and all lines up to and include the EndSection line. The handy thing about this one-liner is that it will match multiple times. In the above example, every “Device” section will be printed from xorg.conf.

Happy perl hacking!

Share and Enjoy:
  • StumbleUpon
  • Facebook
  • Twitter
  • Reddit
Perlisms: Print all lines between two regexes

Leave a Reply

Your email address will not be published.