sed delete all lines after

October 26, 2012

Objective: Prep dev httpd.conf for production

Tasks:

  1. Remove jenkins virtual host entry (which starts with a comment line “#Start Jenkins”)
  2. Replace .dev with nothing (ex: www.dev.example.com becomes www.example.com)
  3. Replace debug with error (ex: Loglevel debug becomes Loglevel error)

Sed to remove lines after match

In this example we will use sed delete all lines after the words “Start Jenkins” (from an httpd.conf file)

cat /etc/httpd/conf/httpd.conf | sed -n '/Start Jenkins/q;p'

Replace “.dev” with nothing

perl -pi -e 's~dev\.~~g' /tmp/file

Replace “debug” with “error”

perl -pi -e 's~debug~error~g' /tmp/file

Put it all together

#!/bin/bash
cat /etc/httpd/conf/httpd.conf | sed -n '/Start Jenkins/q;p' > \
/tmpfile; perl -pi -e 's~dev\.~~g' \
/tmpfile; perl -pi -e 's~debug~error~g' /tmpfile