Linux: Working with sed



Working with sed (a stream editor)


  At some point I had to modify the configuration of multiple nginx configuration files, replacing on all of them a specific string with another one, commenting some strings, deleting some other strings, on one of my CentOS machines.
  If I would go to edit manually file by file, it would take a few hours, at least.
  So, I choose to use sed and to edit all files at the same time and in order to explain, I creates 100 files containing the same string, I'm using "tee" command:
johnyc20@centos:~$ echo "fastcgi_pass 127.0.0.1:9501;"  | tee site-name-{1..100}.conf
  Now I have 100 files containg the "fastcgi_pass 127.0.0.1:9501;":
johnyc20@centos:~$ cat site-name-*
fastcgi_pass          127.0.0.1:9501;
  Now I will explain:
  1. How to replace content
  2. How to comment lines
  3. How to insert lines 
  4. How to delete lines
  5. Short summary of sed options

How to replace content

The first thing, I am replacing the '127.0.0.1' string with '192.168.200.10':
johnyc20@centos:~$ sed -i 's/127.0.0.1/192.168.200.10/g' *.conf 
Now all my files now contain "fastcgi_pass 192.168.200.10:9501;" line:
johnyc20@centos:~$ cat site-name-1.conf 
fastcgi_pass 192.168.200.10:9501;
johnyc20@centos:~$

 How to comment lines

  Let's comment all lines starting with "fastcgi_pass":
johnyc20@centos:~$ sed -i '/fastcgi_pass/s/^/#/' *.conf
And check if it worked:       
johnyc20@centos:~$ cat site-name-1.conf                
#fastcgi_pass 192.168.200.10:9501;
johnyc20@centos:~$ 

 How to insert lines

I am gonna insert the "[tab]fastcgi_pass          127.0.0.1:9501;" after the line starting with "#fastcgi_pass":

johnyc20@centos:~$ sed -i '/#fastcgi_pass/a \\tfastcgi_pass          127.0.0.1:9501;' *.conf
Checking if the new inserted line is there:

johnyc20@centos:~$ cat site-name-1.conf
#fastcgi_pass 192.168.200.10:9501;
        fastcgi_pass          127.0.0.1:9501;

How to delete lines

Now we will delete the lines starting with the "#fastcgi_pass" string:
johnyc20@centos:~$ sed -i '/#fastcgi_pass/d' *.conf
Checking if it worked:

johnyc20@centos:~$ cat site-name-1.conf               
        fastcgi_pass          127.0.0.1:9501;

Short summary of sed arguments and options

Arguments

  • no argument - just output the way that the file will look like, applying the sed changes
  • -i - inline edit, changing the actual file
  • -ie - inline edit, but keep a copy of the original with an "e" on the end of file
's/match/string/g'
  • s - substitute
  • match - the string that will be substitute
  • string - the string that will replace the 'match' string
  • g - global, apply the action for all occurrences of the 'match' string, by default it will match just first occurrence for each line
  • use / or # as separator between fields both 's/match/string/g' and 's#match#string#g' are valid; if the string already has a '/', the use the '#' separator.

Comments

Popular posts from this blog

JunOS - mount USB stick

SSH: Generating and using SSH keys

Linux: Versioning the /etc/ configurations using 'etckeeper'