How do I extract config values from between BEGIN and END markers?

Quick Linux Tip #43:

Try: sed -n '/BEGIN CERTIFICATE/,/END CERTIFICATE/p' /etc/ssl/certs/server.crt

Info: sed -n suppresses automatic printing. The pattern '/START/,/END/p' prints all lines between two matching delimiters (inclusive).

Examples:

  • $ sed -n '/^\[database\]/,/^$/p' config.ini  # Extract section until empty line
  • $ awk '/BEGIN/,/END/' file.txt  # Equivalent using awk
  • $ sed -n '/ERROR/,+5p' logfile  # Print ERROR line plus next 5 lines

Note: Range matching /X/,/Y/ works line-by-line from first match of X to next match of Y. The +N relative line count syntax is a GNU sed extension.




LinuxTeck.com
linuxteck@ubuntu:~$ sed -n '/BEGIN CERTIFICATE/,/END CERTIFICATE/p' /etc/ssl/certs/server.crt
-----BEGIN CERTIFICATE-----
MIIEzzCCA1ugAwIBAgI...
cm5ldGVzM...
-----END CERTIFICATE-----

linuxteck@ubuntu:~$ sed -n '/^\[database\]/,/^$/p' /etc/myapp/config.ini
[database]
host = 127.0.0.1
port = 5432
user = dbuser

linuxteck@ubuntu:~$

PREVIOUS ARTICLE Quick Linux Tip #42: How do I watch a long command run AND save its complete output?
About John Britto

John Britto Founder & Chief-Editor @LinuxTeck. A Computer Geek and Linux Intellectual having more than 20+ years of experience in Linux and Open Source technologies.

View all posts by John Britto →

Leave a Reply

Your email address will not be published.