Sed
Sed
SED command in UNIX is stands for stream editor and it can perform lot’s of
function on file like, searching, find and replace, insertion or deletion. Though
most common use of SED command in UNIX is for substitution or for find and replace.
By using SED you can edit files even without opening it, which is much quicker way
to find and replace something in file, than first opening that file in VI Editor
and then changing it.
•SED is a powerful text stream editor. Can do insertion, deletion, search and
replace(substitution).
•SED command in unix supports regular expression which allows it perform complex
pattern matching.
Syntax:
sed OPTIONS... [SCRIPT] [INPUTFILE...]
Example:
Consider the below text file as an input.
$cat > geekfile.txt
Sample Commands
1.Replacing or substituting string : Sed command is mostly used to replace the text
in a file. The below simple sed command replaces the word “unix” with “linux” in
the file. $sed 's/unix/linux/' geekfile.txt
Output :
Here the “s” specifies the substitution operation. The “/” are delimiters. The
“unix” is the search pattern and the “linux” is the replacement string.
By default, the sed command replaces the first occurrence of the pattern in each
line and it won’t replace the second, third…occurrence in the line.
2.Replacing the nth occurrence of a pattern in a line : Use the /1, /2 etc flags
to replace the first, second occurrence of a pattern in a line. The below command
replaces the second occurrence of the word “unix” with “linux” in a line. $sed
's/unix/linux/2' geekfile.txt
Output:
unix is great os. linux is opensource. unix is free os.
learn operating system.
unix linux which one you choose.
unix is easy to learn.linux is a multiuser os.Learn unix .unix is a powerful.
3.Replacing all the occurrence of the pattern in a line : The substitute flag /g
(global replacement) specifies the sed command to replace all the occurrences of
the string in the line. $sed 's/unix/linux/g' geekfile.txt
Output :
linux is great os. linux is opensource. linux is free os.
learn operating system.
linux linux which one you choose.
linux is easy to learn.linux is a multiuser os.Learn linux .linux is a powerful.
4.Replacing from nth occurrence to all occurrences in a line : Use the combination
of /1, /2 etc and /g to replace all the patterns from the nth occurrence of a
pattern in a line. The following sed command replaces the third, fourth, fifth…
“unix” word with “linux” word in a line. $sed 's/unix/linux/3g' geekfile.txt
Output:
unix is great os. unix is opensource. linux is free os.
learn operating system.
unix linux which one you choose.
unix is easy to learn.unix is a multiuser os.Learn linux .linux is a powerful.
5.Parenthesize first character of each word : This sed example prints the first
character of every word in paranthesis. $ echo "Welcome To The Geek Stuff" | sed
's/\(\b[A-Z]\)/\(\1\)/g'
Output:
(W)elcome (T)o (T)he (G)eek (S)tuff
6.Replacing string on a specific line number : You can restrict the sed command to
replace the string on a specific line number. An example is $sed '3 s/unix/linux/'
geekfile.txt
Output:
unix is great os. unix is opensource. unix is free os.
learn operating system.
linux linux which one you choose.
unix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
The above sed command replaces the string only on the third line.
7.Duplicating the replaced line with /p flag : The /p print flag prints the
replaced line twice on the terminal. If a line does not have the search pattern and
is not replaced, then the /p prints that line only once. $sed 's/unix/linux/p'
geekfile.txt
Output:
linux is great os. unix is opensource. unix is free os.
linux is great os. unix is opensource. unix is free os.
learn operating system.
linux linux which one you choose.
linux linux which one you choose.
linux is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
linux is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
8.Printing only the replaced lines : Use the -n option along with the /p print flag
to display only the replaced lines. Here the -n option suppresses the duplicate
rows generated by the /p flag and prints the replaced lines only one time. $sed -n
's/unix/linux/p' geekfile.txt
Output:
linux is great os. unix is opensource. unix is free os.
linux linux which one you choose.
linux is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
If you use -n alone without /p, then the sed does not print anything.
9.Replacing string on a range of lines : You can specify a range of line numbers
to the sed command for replacing a string. $sed '1,3 s/unix/linux/' geekfile.txt
Output:
linux is great os. unix is opensource. unix is free os.
learn operating system.
linux linux which one you choose.
unix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
Here the sed command replaces the lines with range from 1 to 3. Another example is
$sed '2,$ s/unix/linux/' geekfile.txt
Output:
unix is great os. unix is opensource. unix is free os.
learn operating system.
linux linux which one you choose.
linux is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful
Here $ indicates the last line in the file. So the sed command replaces the text
from second line to last line in the file.
10.Deleting lines from a particular file : SED command can also be used for
deleting lines from a particular file. SED command is used for performing deletion
operation without even opening the file
Examples:
1. To Delete a particular line say n in this example
Syntax:
$ sed 'nd' filename.txt
Example:
$ sed '5d' filename.txt
2. To Delete a last line
Syntax:
$ sed '$d' filename.txt
=================================================
We have discussed some of the SED command options in Sed Command in Linux/Unix with
examples
SED is used for finding, filtering, text substitution, replacement and text
manipulations like insertion, deletion search etc. It’s a one of the powerful
utility offered by Linux/Unix systems. We can use sed with regular expressions. I
hope atleast you have the basic knowledge about Linux regular expressions.
It provides Non-interactive editing of text files thats why it’s used to automate
editing and has two buffers – pattern buffer and hold buffer. Sed use Patter buffer
when it read files, line by line and that currently read line is inserted into
pattern buffer whereas hold buffer is a long-term storage, it catch the
information, store it and reuse it when it is needed. Initially, both are empty.
SED command is used for performing different operation without even opening the
file.
First create a.txt file on which I am going to perform operation for SED commands.
In this blog, I used “a.txt” file to explain all the examples. Blog will become too
long if i write the output of each sed command. So, you may refer the same file to
practice all the commands initially.
sed-output
3 – Delete blank lines and insert one blank line after each line –
[root@rhel7 ~]# sed '/^$/d;G' a.txt
# Numbering lines
1 – Number each line of a file (left alignment). **=** is used to number the
line. \t is used for tab between number and sentence –
[root@rhel7 ~]# sed = a.txt | sed 'N;s/\n/\t/'
# Deleting lines
1 – Delete a particular line –
Syntax: sed ‘nd’ filename
Example :
[root@rhel7 ~]# sed '5d' a.txt
6 – Delete lines starting from nth line and every 2nd line from there –
Syntax: sed ‘n~2d’ filename
Example :
[root@rhel7 ~]# sed '3~2d' a.txt
7 – Delete the lines which matches the pattern and 2 lines after to that –
Syntax: sed ‘/pattern/,+2d’ filename
Example :
[root@rhel7 ~]# sed '/easy/,+2d' a.txt
Pattern Printing
7 – Print the line only which matches the pattern –
Syntax: sed -n /pattern/p filename
Example :
[root@rhel7 ~]# sed -n /every/p a.txt
8 – Print lines which matches the pattern i.e from input to xth line.
Syntax: sed -n ‘/pattern/,xp’ filename
Example :
[root@rhel7 ~]# sed -n '/everyone/,5p' a.txt
Following prints lines which matches the pattern, 3rd line matches the pattern
“everyone”, so it prints from 3rd line to 5th line. Use $ in place of 5, if want to
print the file till end.
9 – Prints lines from the xth line of the input, up-to the line which matches the
pattern. If the pattern doesn’t found then it prints up-to end of the file.
Syntax: sed -n ‘x,/pattern/p’ filename
Example :
sed -n '1,/everyone/p' a.txt
10 – Print the lines which matches the pattern up-to the next xth lines –
Syntax: sed -n ‘/pattern/,+xp’ filename
Example :
We wrote “2” because we replaces the second occurrence. Likewise you can use 3, 4
etc according to need.
Note – This sed command replaces the second, third, etc occurrences of pattern “to”
with “TWO” in a line.
If you wish to print only the replaced lines, then use “-n” option along with “/p”
print flag to display only the replaced lines –
[root@rhel7 ~]# sed -n 's/to/TWO/p' a.txt
And if you wish to print the replaced lines twice, then only use “/p” print flag
without “-n” option-
[root@rhel7 ~]# sed 's/to/TWO/p' a.txt
5 – Replacing pattern on a specific line number. Here, “m” is the line number.
Syntax: sed ‘m s/old_pattern/new_pattern/’ filename
Example :
[root@rhel7 ~]# sed '3 s/every/each/' a.txt
Example :
[root@rhel7 ~]# sed '2,5 s/to/TWO/' a.txt
Note – $ can be used in place of “y” if we wish to change the pattern up-to last
line in the file.
Example :
[root@rhel7 ~]# sed '2,$ s/to/TWO/' a.txt