sed command in Linux with examples

A regular expression is a string that can be used to describe several sequences of characters. Regular expressions are used by several different Unix commands, including ed, sed, awk, grep, and, to a more limited extent, vi.

linux-reemplazar-texto-en-archivos-con-sed-sysadmit

This tutorial would teach you how to use regular expression along with sed.

Printing only the replaced lines

To display only the replaced line we use -n option with /p flag. If you use -n without /p, then It will not print anything.

# sed -n 's/OS/operating system/p' sed.txt

Output

linux is opensource operating system. linux is free OS.

Running multiple sed commands

You can run multiple sed commands using pipe. With the help of pipe the output of one sed command as input to another sed command.

# sed 's/linux/windows/' sed.txt| sed 's/OS/operating system/'

Output

Wants to learn different operating system.
windows is opensource operating system. linux is free OS.
windows perform task fast than windows.
windowswindows which one you choose.
http://techoism.com

Sed also provides -e option to run multiple sed commands without using pipe. The command as below:

# sed -e 's/linux/windows/' -e 's/OS/operating system/' sed.txt

Output

Wants to learn different operating system.
windows is opensource operating system. linux is free OS.
windows perform task fast than windows.
windowswindows which one you choose.
http://techoism.com

Deleting lines

You can delete any specifyic no. of lines from a file by specifying the line number using following command:

#  sed '4 d' sed.txt

Output

Wants to learn different operating system.
linux is opensource OS. linux is free OS.
linux perform task fast than windows.
http://techoism.com

You can delete multiple no. of lines using following command:

# sed '3,$ d' sed.txt

Output

Wants to learn different operating system.
linux is opensource OS. linux is free OS.

Duplicating lines

With the help of sed command you can print each line of a file two times.

# sed 'p' sed.txt

Output

Wants to learn different operating system.
Wants to learn different operating system.
linux is opensource OS. linux is free OS.
linux is opensource OS. linux is free OS.
linux perform task fast than windows.
linux perform task fast than windows.
linuxwindows which one you choose.
linuxwindows which one you choose.
http://techoism.com
http://techoism.com

Search string as grep command

You can make sed command to work as similar to grep command.

# sed -n '/windows/ p' sed.txt

Output

linux perform task fast than windows.
linuxwindows which one you choose.
Here the sed command looks for the pattern "unix" in each line of a file and prints those lines that has the pattern.

You can also make the sed command to work as grep -v, just by using the reversing the sed with NOT (!).

# sed -n '/linux/ !p' sed.txt

Output:

Wants to learn different operating system.
http://techoism.com

Add a line after a match

You can add a new line after a string match using “a” option. Use following command:

# sed '/linux/ a "Unix is also a opensource"' sed.txt

Output:

Wants to learn different operating system.
linux is opensource OS. linux is free OS.
linux perform task fast than windows.
"Unix is also a opensource"
linuxwindows which one you choose.
"Unix is also a opensource"
http://techoism.com

Add a line before a match

You can add a new line before a string match using “i” option. Use following command:

# sed '/windows/ i "Unix is also a opensource"' sed.txt

Output:

Wants to learn different operating system.
linux is opensource OS. linux is free OS.
"Unix is also a opensource"
linux perform task fast than windows.
"Unix is also a opensource"
linuxwindows which one you choose.
http://techoism.com

Change a line

You can replace an entire line with a new line using “c” option.

# sed '/linux/ c "Change line"' sed.txt

Output:

Wants to learn different operating system.
"Change line"
"Change line"
"Change line"
http://techoism.com

Convert the lower case letters to upper case

The sed command can be used to convert the lower case letters to upper case letters by using the transform “y” option.

# sed 'y/ulw/ULW/' sed.txt

Output:

Wants to Learn different operating system.
LinUx is opensoUrce OS. LinUx is free OS.
LinUx perform task fast than WindoWs.
LinUxWindoWs Which one yoU choose.
http://techoism.com

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

The reCAPTCHA verification period has expired. Please reload the page.