sed操作指令

(1)替换 1.txt文本中old为new,代码如下
sed 's/old/new/g ’ 1. txt
(2)打印 1. txt文本第一行至第三行,代码如下
Sed -n ‘1,3p’ 1.txt
(3)打印 1.txt文本中第一行与最后一行,代码如下
Sed -n ‘1p; $p’ 1.txt
(4)删除 1.txt第一行至第三行、删除匹配行至最后一行,代码如下
Sed ‘1,3d’ 1.txt
Sed ‘/aaaa/, $d’ 1.txt

(5)删除 1. txt最后3行及删除最后一行,代码如下:
Sed ‘$d’ 1.txt
[root@localhost songlss]# myfile=9.txt
[root@localhost songlss]# echo KaTeX parse error: Expected 'EOF', got '#' at position 38: …alhost songlss]#̲ A=(sed -n ‘KaTeX parse error: Expected 'EOF', got '#' at position 35: …alhost songlss]#̲ let line=A-3+1
[root@localhost songlss]# sed l i n e , line', d’ $myfile
hhhhhthegfagfvebhrh
thebbsvchbhg
dhggggggthe
ooodsgfgjj
goodsvghfvfrg
ttTHEFGSDViii\nggdgggThe\ngsafgdeffd
ttTHEFGSDViiinggdgggThengsafgdeffd

(6)删除 1.txt最后6行,代码如下:
Sed ‘$d’ 1.txt
(7)在 1.txt查找aaa 所在行,并在其下一行添加word字符,a表示在其下一行添加字符串,代码如下:
Sed ‘/aaa/a word’ 1.txt
(8) 在 1.txt查找bbb 所在行,并在其上一行添加word字符,i表示在其上一行添加字符串,代码如下;
Sed ‘/bbb/i word’ 1.txt

猜你喜欢

转载自blog.csdn.net/limy_yi/article/details/89182328