Sed - Example guide

sed is a very useful stream editor to perform search and replace. Below are some useful tip

1. Usage

sed 's/apple/orange/' file

The above will look for first occurrence of apple and replace with orange in the file and send the output to stdout

2. Make changes to the file

sed -i 's/apple/orange/' file

add -i option will make changes to the file itself

3. All occurrence changes

sed -i 's/apple/orange/g' file

adding g mean global replacement

4. Escape single quote

sed -i 's/'\''apple'\''/orange/' file

You can use '\'' to escape a single quote.

Comments

Popular Posts