Linux Sed command to Replace String in a File with Examples
sed is a linux command that you can use to modify or filter text file. using sed command we can delete specific lines from the file also we can replace the string or text from the file. in the below examples i will show you how i used different types of sed commands with decodingdevops.txt file to replace the strings.
$ cat decodingdevops.txt
1 docker python docker
2 ant shell bash
3 gradle ant shell
4 linux gitlab github
5 maven github linux
6 git shell docker
7 jenkins devops jfrog
8 aws mavne ant
9 nagios splunk datadog
10 devops ant linux
11 docker python git
Delete the lines with SED command
Example 1:
if you want to print the file by deleting first line from the file you can use below sed command. it will not delete the line from the file. just it will print the file by removing that particular line.
$ sed '1d' decodingdevops.txt 2 ant shell bash 3 gradle ant shell 4 linux gitlab github 5 maven github linux 6 git shell docker 7 jenkins devops jfrog 8 aws mavne ant 9 nagios splunk datadog 10 devops ant linux 11 docker python git
Example 2:
to remove particulars lines from the file we use below sed command
$ sed '1,5d' decodingdevops.txt 6 git shell docker 7 jenkins devops jfrog 8 aws mavne ant 9 nagios splunk datadog 10 devops ant linux 11 docker python git
in above example it will print the file by removing the lines from 1 to 5. i,e it will print remaining lines in the file, here those are 6 to 11
Print the lines with SED command
Example 3:
by using below sed command we can print only particular lines from the file.
$ sed -n '1,5p' decodingdevops.txt 1 docker python docker 2 ant shell bash 3 gradle ant shell 4 linux gitlab github 5 maven github linux
in above example it printed the lines from only 1 to 5
Remove the line with particular string using SED Command
in the blow example it will print the file by removing the line which is having the docker string
so we have docker string in 1,6,11 lines it will remove those lines and print the remaining file.
$ sed '/docker/d' decodingdevops.txt 2 ant shell bash 3 gradle ant shell 4 linux gitlab github 5 maven github linux 7 jenkins devops jfrog 8 aws mavne ant 9 nagios splunk datadog 10 devops ant linux
Print The line with Particular string using SED Command
it will print the lines with which is having docker strings
$ sed -n -r '/docker/p' decodingdevops.txt 1 docker python docker 6 git shell docker 11 docker python git
this command works exacly like linux grep command.
$ grep docker decodingdevops.txt 1 docker python docker 6 git shell docker 11 docker python git
Linux Sed Command Substitution
Replace a string in a file In linux with SED Command
$ sed 's/docker/dfd/' decodingdevops.txt 1 dfd python docker 2 ant shell bash 3 gradle ant shell 4 linux gitlab github 5 maven github linux 6 git shell dfd 7 jenkins devops jfrog 8 aws mavne ant 9 nagios splunk datadog 10 devops ant linux 11 dfd python git
This only replace first occurrence of the string in each line. if the same docker strings are there more than 1 time in any line it will replace only first occurrence of docker with 'dfsd' . and this is case sensitive, if you mention DOCKER it will not replace.
Replace all occurrences of strings
by adding some extra flags we can replace all occurrences of strings
$ sed 's/docker/dfd/g' decodingdevops.txt 1 dfd python dfd 2 ant shell bash 3 gradle ant shell 4 linux gitlab github 5 maven github linux 6 git shell dfd 7 jenkins devops jfrog 8 aws mavne ant 9 nagios splunk datadog 10 devops ant linux 11 dfd python git
here you can see i added g in the command which represents global. so by using above command we can replace all occurrences of stings
for case insensitive you mention i flag in the command
$ sed 's/DOCKER/dfd/gi' decodingdevops.txt 1 dfd python dfd 2 ant shell bash 3 gradle ant shell 4 linux gitlab github 5 maven github linux 6 git shell dfd 7 jenkins devops jfrog 8 aws mavne ant 9 nagios splunk datadog 10 devops ant linux 11 dfd python git
Remove a String from the file using SED Command
To remove a particular string from the entire file we can use below sed command
$ sed 's/DOCKER//gi' decodingdevops.txt 1 python 2 ant shell bash 3 gradle ant shell 4 linux gitlab github 5 maven github linux 6 git shell 7 jenkins devops jfrog 8 aws mavne ant 9 nagios splunk datadog 10 devops ant linux 11 python git
just dont mention anything in the second string place.
Replace A string in file using sed command in specific lines
$ sed -r '1,+5 s/docker/dfgfdf/gi' decodingdevops.txt 1 dfgfdf python dfgfdf 2 ant shell bash 3 gradle ant shell 4 linux gitlab github 5 maven github linux 6 git shell dfgfdf 7 jenkins devops jfrog 8 aws mavne ant 9 nagios splunk datadog 10 devops ant linux 11 docker python git
Here it will replace the 'docker' string in lines from the 1st to 6(1+5=6) with 'dfgfdf'