Pages

Monday, August 11, 2014

SED Search and Replace



I am using /etc/nsswitch.conf file in below format  as and example.

# cat  /etc/nsswitch.conf

passwd:         compat
group:          compat
shadow:         compat

hosts:          files
networks:       files

protocols:      db files
services:       db files
ethers:         db files
rpc:            db files

netgroup:       nis


1. How to replace pattern with another using sed ?




# sed 's/netgroup/netgroup1/' /etc/nsswitch.conf


2. How to replace word with another  in line contain keyword using sed ?

Below example will be replacing word files with ldap if the line contain the word  services.

# sed  '/services/s/files/ldap/' /etc/nssswitch.conf

GLOBAL FLAG


3 . How to replace the 1st occurrence of lower case "a" with upper case "A" ?

#  sed 's/a/A/'  /etc/nsswitch.conf


4. How to replace all occurrences of lower case "a" with upper case "A" ?


# sed 's/a/A/g'  /etc/nsswitch.conf



Number Flag



5. How to replace the 2nd occurrence of lower case "a" to upper case "A:" ?

#  sed 's/a/A/2' /etc/nsswitch.conf







No comments:

Post a Comment