Linux sed commands

LORY
3 min readApr 2, 2021

the samples text file

[root@localhost /]# cat /home/sed.txt
use python with nginx to build webapp
python php c java are programming languages
linux is friendly to developer
grep sed and awk is useful to processing text
learn python and php is fun. use python and php to build app is great

Replace word

sed ‘s/python/go/’ /home/sed.txt
use go with nginx to build webapp
go php c java are programming languages
linux is friendly to developer
grep sed and awk is useful to processing text
learn go and php is fun. use python and php to build app is great

only the first ‘python’ is replaced with ‘go’ for each line .

use ‘g’ to replace for all

sed ‘s/python/go/g’ /home/sed.txt
use go with nginx to build webapp
go php c java are programming languages
linux is friendly to developer
grep sed and awk is useful to processing text
learn go and php is fun. use go and php to build app is great

what if only replace 2nd ‘python’ with ‘go’ for each line

sed ‘s/python/go/2’ /home/sed.txt
use python with nginx to build webapp
python php c java are programming languages
linux is friendly to developer
grep sed and awk is useful to processing text
learn python and php is fun. use go and php to build app is great

what if only substitute the last line ? (or pass in 5)

sed ‘$ s/python/go/g’ /home/sed.txt
use python with nginx to build webapp
python php c java are programming languages
linux is friendly to developer
grep sed and awk is useful to processing text
learn go and php is fun. use go and php to build app is great

replace a range of lines (with g option to replace all occurrence in each line )

[root@localhost /]# sed ‘2,3 s/python/go/g’ /home/sed.txt
use python with nginx to build webapp
go php c java are programming languages
linux is friendly to developer
grep sed and awk is useful to processing text
learn python and php is fun. use python and php to build app is great

use -n option to only print out the modified lines (add p at end)

[root@localhost /]# sed -n ‘s/python/go/gp’ /home/sed.txt
use go with nginx to build webapp
go php c java are programming languages
learn go and php is fun. use go and php to build app is great

delete nth line

sed ‘3d’ /home/sed.txt
use python with nginx to build webapp
python php c java are programming languages
grep sed and awk is useful to processing text
learn python and php is fun. use python and php to build app is great

or delete a range of lines

[root@localhost /]# sed ‘1,3d’ /home/sed.txt
grep sed and awk is useful to processing text
learn python and php is fun. use python and php to build app is great

delete the matched lines

[root@localhost /]# sed ‘/python/d’ /home/sed.txt
linux is friendly to developer
grep sed and awk is useful to processing text

insert line after matching line

sed ‘/languages/a ++’ /home/sed.txt
use python with nginx to build webapp
python php c java are programming languages
++
linux is friendly to developer
grep sed and awk is useful to processing text
learn python and php is fun. use python and php to build app is great

add chars after matching keyword (without new line), use & as the matching word

sed ‘s/python/& language/’ /home/sed.txt
use python language with nginx to build webapp
python language php c java are programming languages
linux is friendly to developer
grep sed and awk is useful to processing text
learn python language and php is fun. use python and php to build app is great

use -i option to apply changes (insert, replace, delete) in place

[root@localhost /]# sed -i ‘s/python/& script/g’ /home/sed.txt
[root@localhost /]# cat /home/sed.txt
use python script with nginx to build webapp
python script php c java are programming languages
linux is friendly to developer
grep sed and awk is useful to processing text
learn python script and php is fun. use python script and php to build app is great

Write output to file

$ cat data11.txt
python, php
linux, windows
c, python
$ sed -n '/python/w py.txt' data11.txt
$ cat py.txt
python, php
c, python

1–1 char mapping

cat data.txt
echo 1.
echo 2.
echo 3.
echo 4.
echo 1 again.
sed 'y/123/789/' data8.txt
echo 7.
echo 8.
echo 9.
echo 4.
echo 7 again.

read data from file1 into file2 at the n-th line in file2

cat data1.txt
1
2
cat data2.txt
a
b
sed '3r data1.txt' data2.txt
a
b
1
2
c

--

--

LORY

A channel which focusing on developer growth and self improvement