AMP AMP

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.

Tag : sed command
FAQ
Q
How to replace the n'th occurrence of a pattern in a line using the SED command?
A
You can use the following syntax to replace the n'th occurrence of a pattern in a line using the SED command. For Ex: "sed 's/source/replace/2' file.txt"
Q
When should I NOT use sed?
A
You should not use sed when you have "dedicated" tools which can do the job faster or with an easier syntax. Do not use sed when you only want to: print individual lines, based on patterns w
Q
When should I ignore sed and use awk or Perl instead of that?
A
If you can write the same script in awk or Perl and do it in less time, then use Perl or awk. There's no reason to spend an hour writing and debugging a sed script if you can do it in Perl.
Q
What Do you mean by Sed ?
A
1.”Sed” stands for Stream editor. 2.Sed is a non-interactive editor, written by the late Lee E. McMahon in 1973 or 1974. 3.Instead of altering a file interactively by moving the cursor on t
Q
How to set the pattern to search for is Jul 2 at the beginning of each line using the sed command?
A
You can use the following syntax to set the pattern to search for is Jul 2 at the beginning of each line using the sed command. For Ex: "sed -n '/^Jul 1/ p' /file/path".