Saturday, February 15, 2020

SED and AWK examples

Keeping some weird SED examples that might help people.

This example.  I needed to search for an entry, go down a couple of lines and then do a find replace in that line.

So in the below example, i wanted to find the entry starting with 1001, move down 2 lines, and insert change that line using a substitute.  This was in a file called sip_additional.conf

[1000]
deny=0.0.0.0/0.0.0.0 secret=34e9d4179cff8a74259ebb52994bc844 dtmfmode=rfc2833 canreinvite=no permit=0.0.0.0/0.0.0.0 faxdetect=no [1001] deny=0.0.0.0/0.0.0.0 secret=61476817178bc4051dc30de0c429ba29 dtmfmode=rfc2833 canreinvite=no permit=0.0.0.0/0.0.0.0 faxdetect=no


So I used this command 

sed -i '/1001]/!b;n;n;s/secret=/secret=DISABLED/' sip_additional.conf

This looks for the entry 1000]
Then the ;n;n entries mean move down 2 lines
then we perform a (s)ubstitute secret= to secret=DISALBED

The end result looks like this:

[1000]
deny=0.0.0.0/0.0.0.0 secret=34e9d4179cff8a74259ebb52994bc844 dtmfmode=rfc2833 canreinvite=no permit=0.0.0.0/0.0.0.0 faxdetect=no [1001] deny=0.0.0.0/0.0.0.0 secret=DISABLED61476817178bc4051dc30de0c429ba29 dtmfmode=rfc2833 canreinvite=no permit=0.0.0.0/0.0.0.0 faxdetect=no


##################################################################

This is a typical find/replace in a file
sed -i -r 's/display_errors = Off/display_errors = On/' /etc/php.ini

##################################################################



No comments:

Post a Comment

Feel free to leave a comment! If you have any information that you think should be included, please do so here and I'll get it added in.