Table of Contents

Top#Appendix

How to use sed command in Linux



About sed command




Example

Delete row

$ cat test.txt
123
456
789
abc
def
$ cat test.txt | sed -e "/789/d"
123
456
abc
def


Append row

$ cat test.txt
123
456
abc
def
$ cat test.txt | sed -e "/456/a 789"
123
456
789
abc
def


Insert row

$ cat test.txt
123ab
456ab
abcab
defab
$ cat test.txt | sed -e "/456/i aaa"
123ab
aaa
456ab
abcab
defab


Substitution

$ cat test.txt
123
456
abc
def
$ cat test.txt | sed 's/456/789/g'
123
789
abc
def

$  cat test.txt
Defaults          requiretty
$ cat test.txt |sed 's/Defaults.*requiretty/#Default\trequiretty/g'
#Default        requiretty


Replace if matched

sed -i '/disable/ s/yes/no/' /etc/xinetd.d/tftp"


Reuse

If it is enclosed by \(target character string \), it is partially reused such as \1, \2, \3, … of the replaced character string




appendix

Top#Appendix