Sed Command in Linux/Unix With Examples: Syntax: Example
Sed Command in Linux/Unix With Examples: Syntax: Example
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 :
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:
Additionally, you may want to consider using gi instead of g in order to
ignore character case:
To replace multiple blank spaces with a single space, we will use the output
of ip route show and a pipeline:
Of course, you can indicate a single line through its corresponding number
instead of a range.
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 G myfile.txt
here, option ‘n’ suppresses printing of whole file & option ‘p’ will print only line lines
from 22 to 29.
where ‘N’ is the line number & option ‘d’ will delete the mentioned line number. To
delete the last line of the file, use
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.
To replace ‘danger’ on 2nd occurrence of every line from whole file, use
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 '/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
[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.
This will create a backup copy of the file with extension .bak. You can also use other
extension if you like.
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
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.
# ip route show | sed -n '/src/p' | sed -e 's/ */ /g' | cut -d' ' -f9