分类: Python/Ruby
2011-11-03 11:25:37
SED 常用command
p (sed –n) |
显示行 |
= |
显示行号 =在这里是command |
g |
加空行 |
d |
删除行 |
/pattern/s/原串/替换串 sed '/disable/s/yes/no/g' s/\n// s/aaa/aaabbb/或s/aaa/&bbb s/aaa// |
替换 把含有disable的行中所有(g)的yes换成no ‘回车’换成‘空’,删除回车符,提行,合行 行内aaa后插入词bbb 行内删除词------就是”空词//”替换 |
/zzz/a \xxx\yyy /zzz/a \\ /zzz/a \\yyy |
在匹配行zzz,行内xxx后追加yyy 在匹配行zzz,另起一行追加空行 在匹配行zzz,另起一行追加yyy |
/pattern/r 22.txt' 111.txt | 从文件22.txt读出,然后追加到111.txt |
sed -n '/disable/p' 111.txt |
grep disable 111.txt |
sed -n '/disable/!p' 111.txt | grep -v disable 111.txt |
[macg@machome ~]$ sed -n '=' 111.txt 1 2 3 4 |
[macg@machome ~]$ sed -n
'/disable/='
111.txt 查找文件中含有disable的行的行号 3 9 |
Sed –n ‘=’ 1.txt |
只显示行号 |
sed = 1.txt |
显示行号,同时显示每行内容(没有-n) |
# more 111.txt this is 111file this is 222file disable = yes this is file test1 = yes test2 = yes |
[root@machome macg]# sed
'/disable/s/yes/no/g' 111.txt this is 111file this is 222file disable = no this is file test1 = yes test2 = yes |
# sed 's/yes/no/' 111.txt this is 111file this is 222file disable = no this is file test1 = no test2 = no |
$ sed s/echo/test/ ttt1 test echo echo |
标准替换s 只替换每一行中的第一个“echo”字串 |
$ sed s/echo/test/g ttt1 test test test |
全部替换s/xxx/yyy/g 将每一行中的所有的echo都换成“test” |
sed
"${LINENO}a\
FontPath
\"/usr/share/fonts/ttf/zh_CN/\""
/pattern/a\\yyy
行尾追加(另起一行追加)更常用些
匹配pattern行,并且另起一行追加yyy
[macg@machome ~]$ sed '/^disable/a\\this is a test' 111.txt
将a\\后的内容追加到disable打头的行后面行(另起一行) this is 111file this is 222file disable = yes 本行是空格打头,不是disable打头,所以不匹配^disable this is 333file this is 444file this is file test1 = yes test2 = yes disable=no this is a test |
[macg@machome ~]$ sed '/disable/a\\this is a test'
111.txt 将a\\后的内容追加到含有disable的行后面行(另起一行) this is 111file this is 222file disable = yes this is a test this is 333file this is 444file this is file test1 = yes test2 = yes disable=no this is a test |
[macg@machome ~]$ sed '3a\\ this is a test' 111.txt this is 111file this is 222file disable = yes this is a test this is 333file this is 444file this is file test1 = yes test2 = yes disable=no |
[macg@machome ~]$ sed 's/disable/& this is
test/' 111.txt 实际是用& this is test替换disable 注意&即代表前面的disable this is 111file this is 222file disable this is test = yes this is 333file this is 444file this is file test1 = yes test2 = yes disable this is test=no this is 343file this is 344file |
[macg@machome ~]$ sed 's/disable = yes/& this is
test/' 111.txt this is 111file this is 222file disable = yes this is test |
[macg@machome ~]$ more 22.txt 222222222222file [macg@machome ~]$ more 111.txt this is 111file this is 222file disable = yes |
[macg@machome ~]$ sed '/disable/r 22.txt' 111.txt this is 111file this is 222file disable = yes 222222222222file |
所以不需要/pattern/
sed
'1,10d'
删除文件中开头的10行
1,10 逗号是“从…到…”
sed
'$d'
删除文件中的最后一行
$表示最后一行
常见的两种数字标识行号用法
[root@machome macg]# sed -n '2p'
111.txt
this is
222file
$ sed -n '2,7p'
111.txt
逗号2,7相当于from … to
this is 222file
disable = yes
this is 333file
this is 444file
this is file
test1 = yes
sed '2,$d'
111.txt
删除111.txt文件的第2行到末尾所有行
注意'2,7'与'2p;7p'的区别
'2,7p’表示2至7行
'2p;7p’显示2,7行
Command ‘=’ 显示行号
$ sed -n '/disable/=' 111.txt| sed -n '$p'
9
查找文件中含有disable的行的最后一行的行号
sed -n
'$='
file 计算文件行数 (模拟
"wc -l")
$ -----------匹配文件最后一行
= -----------显示当前行号
说是计算行号,实际是显示最后一行的行号
# sed -n '$=' 111.txt 5 |