SED Command in Linux with Examples
sed Command ( Stream Editor )
sed command is a Stream Editor which is used for modifying the files in Linux. Sed helps to make changes to the file.
Example
This file can be taken as an example file for using this command.
[root@linuxhelp user1]# cat example.txt
unix is great os. unix is opensource. unix is free os.
learn operating system.
unixlinux which one you choose.
To replace the word ' Unix' with ' Linux'
[root@linuxhelp user1]# sed ' s/unix/linux/' example.txt
linux is great os. unix is opensource. unix is free os.
learn operating system.
linuxlinux which one you choose.
Options
s &ndash substitution operation
/ - delimiters
Default: It will replace the first occurrence in the line only.
Replacing the specific occurrence in a line
To replace the second occurrence of ' Unix' with ' Linux' in a line.
[root@linuxhelp user1]# sed ' s/unix/linux/2' example.txt
unix is great os. linux is opensource. unix is free os.
learn operating system.
unixlinux which one you choose.
Options
/2 &ndash to notify the second occurrence
Replacing the entire occurrence in a line
To replace ' Unix' with ' Linux' for all occurrence in a line.
[root@ linuxhelp ~]# sed ' s/unix/linux/g' example.txt
linux is great os. linux is opensource. linux is free os.
learn operating system.
linuxlinux which one you choose.
Options
/g &ndash global replacement
Replacing from the specific occurrence to all
To replace all ' Unix' with ' Linux' from the given occurrence to the entire occurrence following back.
[root@ linuxhelp ~]# sed ' s/unix/linux/3g' example.txt
unix is great os. unix is opensource. linux is free os.
learn operating system.
unixlinux which one you choose.
Options
/3g &ndash to notify the change to be made from third occurrence.
Using ' & ' flag to mention string
There may be need to replace the pattern by adding some characters to it. To find the specific word that need to make change, this ' & ' is used.
[root@ linuxhelp ~]# sed ' s/unix/{& }/' example.txt
{unix} is great os. unix is opensource. unix is free os.
learn operating system.
{unix}linux which one you choose.
Duplicates the replaced line
If the change is made then the line will be displayed twice in terminal. If not, it remains the same.
[root@ linuxhelp ~]# sed ' s/unix/linux/p' example.txt
unix is great os. unix is opensource. unix is free os.
unix is great os. unix is opensource. unix is free os.
learn operating system.
linuxlinux which one you choose.
linuxlinux which one you choose.
Options
/p &ndash print flag to display twice.
Printing only replaced line
If the pattern is altered then the specific line will be displayed alone.
[root@ linuxhelp ~]# sed -n ' s/unix/linux/p' example.txt
linux is great os. unix is opensource. unix is free os.
linuxlinux which one you choose.
Options
-n with /p &ndash to display twice the altered line alone.
Nested sed
Running multiple sed conditions in a single command, i.e., taking the output of first instance as the input for second instance and so on.
[root@ linuxhelp ~]# sed ' s/unix/linux/' example.txt | sed ' s/os/system/'
linux is great system. unix is opensource. unix is free os.
learn operating system.
linuxlinux which one you chosysteme.
Another method
[root@ linuxhelp ~]# sed -e ' s/unix/linux/' -e ' s/is/are/' example.txt
linux is great system. unix is opensource. unix is free os.
learn operating system.
linuxlinux which one you chosysteme.
Options
e &ndash To run multiple sed commands.
Replacing a word in specified line number
The command can be made to execute only in the specific line number.
[root@ linuxhelp ~]# sed ' 3 s/unix/linux/' example.txt
unix is great os. unix is opensource. unix is free os.
learn operating system.
linuxlinux which one you choose.
Options
3 &ndash it mentions the line number.
Replacing words in the range of lines
You can specify a range of line numbers to execute the command.
[root@ linuxhelp ~]# sed ' 1,3 s/unix/linux/' example.txt
linux is great os. unix is opensource. unix is free os.
learn operating system.
linuxlinux which one you choose.
Options
1, 3 &ndash it mentions the range of line numbers.
Another method
[root@ linuxhelp ~]# sed ' 2,$ s/unix/linux/' example.txt
linux is great os. unix is opensource. unix is free os.
learn operating system.
linuxlinux which one you choose.
Here, ‘ $’ indicates the last line, so the changes is made from 2nd line to last line.
Replace if pattern matches
Specify a pattern in sed command. If pattern match occurs then sed command looks for the string to be replaced.
[root@ linuxhelp ~]# sed ' /linux/ s/unix/centos/' example.txt
unix is great os. unix is opensource. unix is free os.
learn operating system.
centoslinux which one you choose.
Here, it looks for the pattern Linux and then replace the word Unix with centos.
Deleting lines
You can delete the lines of a file by specifying line number or a range.
[root@ linuxhelp ~]# sed ' 2 d' example.txt
unix is great os. unix is opensource. unix is free os.
unixlinux which one you choose.
To delete the range of lines
[root@ linuxhelp ~]# sed ' 2,$ d' example.txt
unix is great os. unix is opensource. unix is free os.
Options:
d - Delete
$ - to indicate last line
Adding a new line after a match
sed command can add a new line after a pattern match is found.
[root@ linuxhelp ~]# sed ' /unix/ a " new line" ' example.txt
unix is great os. unix is opensource. unix is free os.
“ new line”
learn operating system.
unixlinux which one you choose.
“ new line”
Options
a &ndash to add a new line after a match is found.
Adding a new line before a match
It can add a new line before a pattern is found.
[root@ linuxhelp ~]# sed ' /unix/ i " new line" ' example.txt
“ new line”
unix is great os. unix is opensource. unix is free os.
learn operating system.
“ new line”
Unixlinux which one you choose.
Options
i &ndash To add a new line before a match is found.
Replacing entire line
sed command can be used to replace the entire line with a new line.
[root@ linuxhelp ~]# sed ' /linux/ c " change of line" ' example.txt
unix is great os. unix is opensource. unix is free os.
learn operating system.
“ change of line”
Options
c &ndash To notify change of line.
Case-sensitive option
sed command can be used to convert lower case to upper case letters.
[root@ linuxhelp ~]# sed ' y/ul/UL/' example.txt
Unix is great os. Unix is opensoUrce. Unix is free os.
Learn operating system.
UnixLinux which one yoU choose.
Options
y &ndash to transform the case of letters.
rh &ndash letters to change from lower case to upper case in a file.
RH &ndash to notify the upper-case option.