Sed Command in Linux/Unix with examples
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
unix is great os. unix is opensource. unix is free os.
learn operating system.
unix linux which one you choose.
unix is easy to learn.unix is a multiuser os.Learn unix .unix is a
powerful.
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.
2. $sed '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.
linux is easy to learn.unix is a multiuser os.Learn unix .unix is a
powerful.
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.
3. 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.
4. $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.
5. 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.
6. $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.
7. 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.
8. $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.
9. Parenthesize first character of each word : This sed example prints the first
character of every word in paranthesis.
10. $ echo "Welcome To The Geek Stuff" | sed 's/\(\b[A-Z]\)/\(\1\)/g'
Output:
(W)elcome (T)o (T)he (G)eek (S)tuff
11. 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
12. $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.
13. 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.
14. $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.
15. 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.
16. $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.
17. Replacing string on a range of lines : You can specify a range of line
numbers to the sed command for replacing a string.
18. $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.
19. 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
20. Syntax:
21. $ sed 'nd' filename.txt
22. Example:
23. $ sed '5d' filename.txt
2. To Delete a last line
Syntax:
$ sed '$d' filename.txt
3. To Delete line from range x to y
Syntax:
$ sed 'x,yd' filename.txt
Example:
$ sed '3,6d' filename.txt
5. To Delete from nth to last line
Syntax:
$ sed 'nth,$d' filename.txt
Example:
$ sed '12,$d' filename.txt
6. To Delete pattern matching line
Syntax:
$ sed '/pattern/d' filename.txt
Example:
$ sed '/abc/d' filename.txt
15 Useful ‘sed’ Command Tips and
Tricks for Daily Linux System
Administration Tasks
1. Viewing a range of lines of a document
Tools such as head and tail allow us to view the bottom or the top of a file.
What if we need to view a section in the middle? The following sed one-
liner will return lines 5 through 10 from myfile.txt:
# sed -n '5,10p' myfile.txt
2. Viewing the entire file except a given
range
On the other hand, it’s possible that you want to print the entire file except a
certain range. To exclude lines 20through 35 from myfile.txt, do:
# sed '20,35d' myfile.txt
3. Viewing non-consecutive lines and
ranges
It’s possible that you’re interested in set of non-consecutive lines, or in
more than one range. Let’s display lines 5-7 and 10-13 from myfile.txt:
# sed -n -e '5,7p' -e '10,13p' myfile.txt
As you can see, the -e option allows us to execute a given action (in this
case, print lines) for each range.
4. Replacing words or characters (basic
substitution)
To replace every instance of the word version with story in myfile.txt, do:
# sed 's/version/story/g' myfile.txt
Additionally, you may want to consider using gi instead of g in order to
ignore character case:
# sed 's/version/story/gi' myfile.txt
To replace multiple blank spaces with a single space, we will use the output
of ip route show and a pipeline:
# ip route show | sed 's/ */ /g'
Compare the output of ip route show with and without the pipeline:
Replace Words or Characters in File
5. Replacing words or characters inside a
range
If you’re interested in replacing words only within a line range
(30 through 40, for example), you can do:
# sed '30,40 s/version/story/g' myfile.txt
Of course, you can indicate a single line through its corresponding number
instead of a range.
6. Using regular expressions (advanced
substitution) – I
Sometimes configuration files are loaded with comments. While this is
certainly useful, it may be helpful to display only the configuration directives
sometimes if you want to view them all at a glance.
To remove empty lines or those beginning with # from the Apache
configuration file, do:
# sed '/^#\|^$\| *#/d' httpd.conf
The caret sign followed by the number sign (^#) indicates the beginning of
a line, whereas ^$ represents blank lines. The vertical bars indicate
boolean operations, whereas the backward slash is used to escape the
vertical bars.
In this particular case, the Apache configuration file has lines with #’s not at
the beginning of some lines, so *# is used to remove those as well.
7. Using regular expressions (advanced
substitution) – II
To replace a word beginning with uppercase or lowercase with another
word, we can also use sed. To illustrate, let’s replace the
word zip or Zip with rar in myfile.txt:
# sed 's/[Zz]ip/rar/g' myfile.txt
Don’t Miss: Use Awk with Regular Expressions to Filter Text in Files
8. Viewing lines containing with a given
pattern
Another use of sed consists in printing the lines from a file that match a
given regular expression. For example, we may be interested in viewing the
authorization and authentication activities that took place on July 2, as per
the /var/log/secure log in a CentOS 7 server.
In this case, the pattern to search for is Jul 2 at the beginning of each line:
# sed -n '/^Jul 1/ p' /var/log/secure
View Logs (Lines) of Particular Date
9. Inserting spaces in files
With sed, we can also insert spaces (blank lines) for each non-empty line in
a file. To insert one blank line every other line in LICENSE, a plain text file,
do:
# sed G myfile.txt
To insert two blank lines, do:
# sed 'G;G' myfile.txt
Add an uppercase G separated by a semicolon if you want to add more
blank lines. The following image illustrates the example outlined in this tip:
Insert Spaces in File
This tip may come in handy if you want to inspect a large configuration file.
Inserting a blank space every other line and piping the output to less will
result in a more-friendly reading experience.
10. Emulating dos2unix with inline editing
The dos2unix program converts plain text files from Windows/Mac
formatting to Unix/Linux, removing hidden newline characters inserted by
some text editors used in those platforms. If it is not installed in your Linux
system, you can mimic its functionality with sed instead of installing it.
In the image at the left we can see several DOS newline characters (^M) ,
which were later removed with:
# sed -i 's/\r//' myfile.txt
Covert Text Files from Windows to Linux
Please note that the -i option indicate in-place editing. Then changes will
not be returned to the screen, but will be saved to the file.
Note: You can insert DOS newline characters while editing a file in vim
editor with Ctrl+V and Ctrl+M .
11. In-place editing and backing up original
file
In the previous tip we used sed to modify a file but did not save the original
file. Sometimes it’s a good idea to save a backup copy of the original file
just in case.
To do that, indicate a suffix following the -i option (inside single quotes) to
be used to rename the original file.
In the following example we will replace all instances
of this or This (ignoring case) with that in myfile.txt, and we will save the
original file as myfile.txt.orig.
Finally, we will use diff utility to identify the differences between both files:#
# sed -i'.orig' 's/this/that/gi' myfile.txt
sed OPTIONS… [SCRIPT] [INPUTFILE…]
Now let’s see some examples.
Example :1) Displaying partial text of a file
With sed, we can view only some part of a file rather than seeing whole file. To see
some lines of the file, use the following command,
[linuxtechi@localhost ~]$ sed -n 22,29p testfile.txt
here, option ‘n’ suppresses printing of whole file & option ‘p’ will print only line lines
from 22 to 29.
Example :2) Display all except some lines
To display all content of a file except for some portion, use the following command,
[linuxtechi@localhost ~]$ sed 22,29d testfile.txt
Option ‘d’ will remove the mentioned lines from output.
Example :3) Display every 3rd line starting with
Nth line
Do display content of every 3rd line starting with line number 2 or any other line, use
the following command
[linuxtechi@localhost ~]$ sed -n '2-3p' file.txt
Example :4 ) Deleting a line using sed command
To delete a line with sed from a file, use the following command,
[linuxtechi@localhost ~]$ sed Nd testfile.txt
where ‘N’ is the line number & option ‘d’ will delete the mentioned line number. To
delete the last line of the file, use
[linuxtechi@localhost ~]$ sed $d testfile.txt
Example :5) Deleting a range of lines
To delete a range of lines from the file, run
[linuxtechi@localhost ~]$ sed '29-34d' testfile.txt
This will delete lines 29 to 34 from testfile.txt file.
Example :6) Deleting lines other than the
mentioned
To delete lines other than the mentioned lines from a file, we will use ‘!’
[linuxtechi@localhost ~]$ sed '29-34!d' testfile.txt
here ‘!’ option is used as not, so it will reverse the condition i.e. will not delete the
lines mentioned. All the lines other 29-34 will be deleted from the files testfile.txt.
Example :7) Adding Blank lines/spaces
To add a blank line after every non-blank line, we will use option ‘G’,
[linuxtechi@localhost ~]$ sed G testfile.txt
Example :8) Search and Replacing a string using
sed
To search & replace a string from the file, we will use the following example,
[linuxtechi@localhost ~]$ sed 's/danger/safety/' testfile.txt
here option ‘s’ will search for word ‘danger’ & replace it with ‘safety’ on every line for
the first occurrence only.
Example :9) Search and replace a string from
whole file using sed
To replace the word completely from the file, we will use option ‘g’ with ‘s’,
[linuxtechi@localhost ~]$ sed 's/danger/safety/g' testfile.txt
Example :10) Replace the nth occurrence of
string pattern
We can also substitute a string on nth occurrence from a file. Like replace ‘danger’
with ‘safety’ only on second occurrence,
[linuxtechi@localhost ~]$ sed ‘s/danger/safety/2’ testfile.txt
To replace ‘danger’ on 2nd occurrence of every line from whole file, use
[linuxtechi@localhost ~]$ sed 's/danger/safety/2g' testfile.txt
Example :11) Replace a string on a particular
line
To replace a string only from a particular line, use
[linuxtechi@localhost ~]$ sed '4 s/danger/safety/' testfile.txt
This will only substitute the string from 4th line of the file. We can also mention a
range of lines instead of a single line,
[linuxtechi@localhost ~]$ sed '4-9 s/danger/safety/' testfile.txt
Example :12) Add a line after/before the matched
search
To add a new line with some content after every pattern match, use option ‘a’ ,
[linuxtechi@localhost ~]$ sed '/danger/a "This is new line with text after match"'
testfile.txt
To add a new line with some content a before every pattern match, use option ‘i’,
[linuxtechi@localhost ~]$ sed '/danger/i "This is new line with text before match"
' testfile.txt
Example :13) Change a whole line with matched
pattern
To change a whole line to a new line when a search pattern matches we need to use
option ‘c’ with sed,
[linuxtechi@localhost ~]$ sed '/danger/c "This will be the new line" ' testfile.txt
So when the pattern matches ‘danger’, whole line will be changed to the mentioned
line.
Advanced options with sed
Up until now we were only using simple expressions with sed, now we will discuss
some advanced uses of sed with regex,
Example :14) Running multiple sed commands
If we need to perform multiple sed expressions, we can use option ‘e’ to chain the sed
commands,
[linuxtechi@localhost ~]$ sed -e 's/danger/safety/g' -e 's/hate/love/'
testfile.txt
Example :15) Making a backup copy before
editing a file
To create a backup copy of a file before we edit it, use option ‘-i.bak’,
[linuxtechi@localhost ~]$ sed -i.bak -e 's/danger/safety/g' testfile.txt
This will create a backup copy of the file with extension .bak. You can also use other
extension if you like.
Example :16) Delete a file line starting with &
ending with a pattern
To delete a file line starting with a particular string & ending with another string, use
[linuxtechi@localhost ~]$ sed -e 's/danger.*stops//g' testfile.txt
This will delete the line with ‘danger’ on start & ‘stops’ in the end & it can have any
number of words in between , ‘.*’ defines that part.
Example :17) Appending lines
To add some content before every line with sed & regex, use
[linuxtechi@localhost ~]$ sed -e 's/.*/testing sed &/' testfile.txt
So now every line will have ‘testing sed’ before it.
Example :18) Removing all commented lines &
empty lines
To remove all commented lines i.e. lines with # & all the empty lines, use
[linuxtechi@localhost ~]$ sed -e 's/#.*//;/^$/d' testfile.txt
To only remove commented lines, use
[linuxtechi@localhost ~]$ sed -e 's/#.*//' testfile.txt
Example :19) Get list of all usernames from
/etc/passwd
To get the list of all usernames from /etc/passwd file, use
[linuxtechi@localhost ~]$ sed 's/\([^:]*\).*/\1/' /etc/passwd
a complete list all usernames will be generated on screen as output.
Example :20) Prevent overwriting of system
links with sed command
‘sed -i’ command has been know to remove system links & create only regular files in
place of the link file. So to avoid such a situation & prevent ‘sed -i‘ from destroying
the links, use ‘–follow-symklinks‘ options with the command being executed.
Let’s assume i want to disable SELinux on CentOS or RHEL Severs
[linuxtechi@localhost ~]# sed -i --follow-symlinks
's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux
These were some examples to show sed, we can use these reference to employ them
as & when needed. If you guys have any queries related to this or any article, do share
with us.
Use sed to Edit and Backup Original File
12. Switching pairs of words
Let’s suppose you have a file containing full names in the format First
name, Last name. To adequately process the file, you may want to
switch Last name and First name.
We can do that with sed fairly easily:
# sed 's/^\(.*\),\(.*\)$/\, /g' names.txt
Switch Words in File
In the image above we can see that parentheses, being special characters,
need to be escaped, as do the numbers 1 and 2.
These numbers represent the highlighted regular expressions (which need
to appear inside parentheses):
1. 1 represents the beginning of each line up to the comma.
2. 2 is a placeholder for everything that is right of the comma to the end
of the line.
The desired output is indicated in the format SecondColumn (Last name)
+ comma + space + FirstColumn(First name). Feel free to change it to
whatever you wish.
13. Replacing words only if a separate
match is found
Sometimes replacing all instances of a given word, or a random few, is not
precisely what we need. Perhaps we need to perform the replacement if a
separate match is found.
For example, we may want to replace start with stop only if the word
services is found in the same line. In that scenario, here’s what will happen:
We need to start partying at work,
but let’s remember to start all services first.
In the first line, start will not be replaced with stop since the word services
does not appear in that line, as opposed to the second line.
# sed '/services/ s/start/stop/g' msg.txt
Replace Words in File
14. Performing two or more substitutions at
once
You can combine two or more substitutions one single sed command. Let’s
replace the words that and line in myfile.txt with This and verse,
respectively.
Note how this can be done by using an ordinary sed substitution command
followed by a semicolon and a second substitution command:
# sed -i 's/that/this/gi;s/line/verse/gi' myfile.txt
This tip is illustrated in the following image:
Replace Multiple Words in File
15. Combining sed and other commands
Of course, sed can be combined with other tools in order to create more
powerful commands. For example, let’s use the example given in TIP
#4 and extract our IP address from the output of the ip route command.
We will begin by printing only the line where the word src is. Then we will
convert multiple spaces into a single one. Finally, we will cut the 9th field
(considering a single space as field separator), which is where the IP
address is:
# ip route show | sed -n '/src/p' | sed -e 's/ */ /g' | cut -d' ' -f9
The image below illustrates each step of the above command:
Combine sed with Other Commands