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.
The 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 OPTIONS... [SCRIPT] [INPUTFILE...]
The following sed
command will return line from 2 to 12 from myfile.txt
# sed -n '2,12p' myfile.txt
Exclude lines 15 through 30 from myfile.txt.
# sed '15,30d' myfile.txt
Example for lines 5-10 and 15-20 from myfile.txt:
# sed -n -e '5,10p' -e '15,20p' myfile.txt
The -e
option allows us to execute a given action (in this case, print lines) for each range.
The substitute flag /g (global replacement) specifies the sed command to replace all the occurrences of the string in the line.
Replace every instance of the word story
with JIRA
in myfile.txt :
# sed 's/story/JIRA/g' myfile.txt
Note −> To ignore character case use gi
# sed 's/story/JIRA/gi' myfile.txt
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 "story" with "JIRA" in a line.
# sed 's/story/JIRA/2' myfile.txt
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… "story" word with "JIRA" word in a line.
# sed 's/story/JIRA/3g' myfile.txt
You can restrict the sed command to replace the string on a specific line number. An example to replace the string only on the fifth line.
# sed '5 s/story/JIRA/' myfile.txt
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/story/JIRA/p' myfile.txt
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/story/JIRA/p' myfile.txt
You can specify a range of line numbers to the sed command for replacing a string.
Below example replaces story
with JIRA
only within a line range 3 through 5.
sed '3,5 s/story/JIRA/g' myfile.txt
Below is the example to replace story
with JIRA
from line 2 to the last line in the file
# sed '2,$ s/story/JIRA/g' myfile.txt
Here $
indicates last line in the file.
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' myfile.txt
You can combine two or more substitutions one single sed command. Let’s replace the words story
with JIRA
and bug
with hotfix
.
This can be done by using an ordinary sed substitution command followed by a semicolon and a second substitution command:
# sed -i 's/story/JIRA/gi;s/bug/hotfix/gi' myfile.txt
# sed '10d' myfile.txt
# sed '$d' myfile.txt
# sed '2,8d' myfile.txt
# sed '5,$d' myfile.txt
# sed 'xyz/d' myfile.txt
If you like dEexams.com and would like to contribute, you can write your article here or mail your article to admin@deexams.com . See your article appearing on the dEexams.com main page and help others to learn.