sed 顯示檔案行號內容的用法
阿新 • • 發佈:2018-11-25
[[email protected] ~]# cat >person.txt<<EOF
> 101,oldboy,CEO
> 102,zhangyao,CTO
> 103,Alex,C00
> 104,yy,CFO
> 105,feixue,CTO
> EOF
[email protected] ~]# mkdir -p /oldboy
[[email protected] oldboy]# cat person.txt
101,oldboy,CEO
102,zhangyao,CTO
103,Alex,C00
104,yy,CFO
105,feixue,CTO
[ [email protected] oldboy]# sed '1p' person.txt 顯示所有內容
101,oldboy,CEO
101,oldboy,CEO
102,zhangyao,CTO
103,Alex,C00
104,yy,CFO
105,feixue,CTO
1.顯示某一行,-n 取消預設輸出
[[email protected] oldboy]# sed -n '1p' person.txt
101,oldboy,CEO
[[email protected] oldboy]# sed -n '3p' person.txt
103,Alex,C00
2.顯示連續多行的內容,表示顯示行的範圍包含首位行
[ [email protected] oldboy]# sed -n '1,2p' person.txt
101,oldboy,CEO
102,zhangyao,CTO
[[email protected] oldboy]# sed -n '2,5p' person.txt(注意,2,5 表示的是連續的2,3,4,5行內容強調的是連續性)
102,zhangyao,CTO
103,Alex,C00
104,yy,CFO
105,feixue,CTO
3.顯示檔案中包行oldboy的行
[[email protected] oldboy]# sed -n '/oldboy/p' person.txt
101,oldboy,CEO
[[email protected] oldboy]# grep 'oldboy' person.txt
4.顯示檔案中包行oldboy的行到包含104的行。
[[email protected] oldboy]# sed -n '/oldboy/,/^104/p' person.txt
101,oldboy,CEO
102,zhangyao,CTO
103,Alex,C00
104,yy,CFO
[[email protected] oldboy]# grep '^104' person.txt9(grep查詢的時候不能顯示範圍)
104,yy,CFO
[[email protected] oldboy]# sed -n '/^104/p' person.txt (^括號表示更精確一點)
104,yy,CFO
[[email protected] oldboy]# sed -n '/^101/,/^105/p' person.txt
101,oldboy,CEO
102,zhangyao,CTO
103,Alex,C00
104,yy,CFO
105,feixue,CTO
5.查詢多個條件的方法
[[email protected] oldboy]# egrep 'oldboy|yy' person.txt
101,oldboy,CEO
104,yy,CFO
[[email protected] oldboy]# sed -rn '/oldboy|yy/p' person.txt (-rn -r 表示查詢多個條件的內容)
101,oldboy,CEO
104,yy,CFO
[[email protected] oldboy]# sed -n '1p;3p;5p' person.txt ( ;號表示中間不是連續行的內容)
101,oldboy,CEO
103,Alex,C00
105,feixue,CTO
[[email protected] oldboy]# sed -n '/oldboy/p;/yy/p;/feixue/p' person.txt (顯示多行不連續檔案的內容)
101,oldboy,CEO
104,yy,CFO
105,feixue,CTO
sed 查詢的時候都是結合p (preint) 一起來使用
[[email protected] oldboy]# sed '3a yangjuncheng' person.txt (3 表示第三行 a表示增加 3a表示在第三行的後面新增一行)
101,oldboy,CEO
102,zhangyao,CTO
103,Alex,C00
yangjuncheng
104,yy,CFO
105,feixue,CTO
[[email protected] oldboy]# sed '3i aaaaaaaaa' person.txt (i 表示在前面插入內容,如果要寫入檔案是在sed後面加入i,將新增的內容寫到檔案中)
101,oldboy,CEO
102,zhangyao,CTO
aaaaaaaaa
103,Alex,C00
104,yy,CFO
105,feixue,CTO
[[email protected] oldboy]# sed -n '$p' person.txt ($p 表示最後一行)
105,feixue,CTO
[[email protected] oldboy]# sed '$a new,new,new' person.txt
101,oldboy,CEO
102,zhangyao,CTO
103,Alex,C00
104,yy,CFO
105,feixue,CTO
new,new,new
[[email protected] oldboy]# sed '$a new,new,new\nold,old,old' person.txt(檔案的最後一行/n 表示在後面增加多行的內容)
101,oldboy,CEO
102,zhangyao,CTO
103,Alex,C00
104,yy,CFO
105,feixue,CTO
new,new,new
old,old,old
[[email protected] oldboy]# sed -i '$a new,new,new\nold,old,old' person.txt (-i 表示寫入檔案內容中)
[[email protected] oldboy]# cat person.txt
101,oldboy,CEO
102,zhangyao,CTO
103,Alex,C00
104,yy,CFO
105,feixue,CTO
new,new,new
old,old,old
cat >>person.txt <<EOF (在一個檔案最後一行新增多行的方法)
wwwww
cccccccc
dddddddd
QQQQQQ
WWWWW
yang jun cheng
wang bing bing
[[email protected] oldboy]# echo test test >>person.txt (如果在檔案的最後面追加的檔案內容不是很多的時候用echo就行,如果多行就用cat 方法)
[[email protected] oldboy]# cat person.txt
yang jun cheng
wang bing bing
li qang
test test
[[email protected] oldboy]# sed '$d' persion.txt
101,oldboy,CEO
102,zhangyao,CTO
103,Alex,C00
104,yy,CFO
105,feixue,CTO
new,new,new
[[email protected] oldboy]# sed '2,3d' persion.txt (刪除2到3行)
101,oldboy,CEO
104,yy,CFO
105,feixue,CTO
企業案例:不顯示檔案的空行
[[email protected] oldboy]# cat -n persion.txt
1 101,oldboy,CEO
2
3 102,zhangyao,CTO
4
5 103,Alex,C00
6
7 104,yy,CFO
8
9 105,feixue,CTO
[[email protected] oldboy]# cat -A persion.txt (-A 表示不帶空格的行)
101,oldboy,CEO$
$
102,zhangyao,CTO$
$
103,Alex,C00$
$
104,yy,CFO$
$
[[email protected] oldboy]# grep -v '^$' persion.txt (過濾掉空行)
101,oldboy,CEO
102,zhangyao,CTO
103,Alex,C00
104,yy,CFO
105,feixue,CTO
[[email protected] oldboy]# egrep -v '^$' persion.txt(過濾掉空行)
101,oldboy,CEO
102,zhangyao,CTO
103,Alex,C00
104,yy,CFO
105,feixue,CTO
[[email protected] oldboy]# sed '/^$/d' persion.txt (刪除空行的方法,如果要寫入檔案加 -i )
101,oldboy,CEO
102,zhangyao,CTO
103,Alex,C00
104,yy,CFO
105,feixue,CTO
[[email protected] oldboy]# sed -n '/^$/p' persion.txt (顯示檔案的空行)
[[email protected] oldboy]# sed -n '/^$/!p' persion.txt (排除空行方法)
101,oldboy,CEO
102,zhangyao,CTO
103,Alex,C00
104,yy,CFO
105,feixue,CTO
[[email protected] oldboy]# awk '!/^$/' persion.txt (排除空行的方法)
101,oldboy,CEO
102,zhangyao,CTO
103,Alex,C00
104,yy,CFO
105,feixue,CTO
變數引用
[[email protected] oldboy]# x=oldboy
[[email protected] oldboy]# y=oldgirl
[[email protected] oldboy]# sed "s#$x#$y#g" persion.txt (這裡變數的引用必須要用雙引號才行,單引號識別不出來)
101,oldgirl,CEO
102,zhangyao,CTO
103,Alex,C00
104,yy,CFO
105,feixue,CTO
[[email protected] oldboy]# sed "s#oldboy#oldgirl#g" persion.txt
101,oldgirl,CEO
102,zhangyao,CTO
103,Alex,C00
104,yy,CFO
105,feixue,CTO
[[email protected] ~]# cat >person.txt<<EOF
> 101,oldboy,CEO
> 102,zhangyao,CTO
> 103,Alex,C00
> 104,yy,CFO
> 105,feixue,CTO
> EOF
[email protected] ~]# mkdir -p /oldboy
[[email protected] oldboy]# cat person.txt
101,oldboy,CEO
102,zhangyao,CTO
103,Alex,C00
104,yy,CFO
105,feixue,CTO
[[email protected] oldboy]# sed '1p' person.txt 顯示所有內容
101,oldboy,CEO
101,oldboy,CEO
102,zhangyao,CTO
103,Alex,C00
104,yy,CFO
105,feixue,CTO
1.顯示某一行,-n 取消預設輸出
[[email protected] oldboy]# sed -n '1p' person.txt
101,oldboy,CEO
[[email protected] oldboy]# sed -n '3p' person.txt
103,Alex,C00
2.顯示連續多行的內容,表示顯示行的範圍包含首位行
[[email protected] oldboy]# sed -n '1,2p' person.txt
101,oldboy,CEO
102,zhangyao,CTO
[[email protected] oldboy]# sed -n '2,5p' person.txt(注意,2,5 表示的是連續的2,3,4,5行內容強調的是連續性)
102,zhangyao,CTO
103,Alex,C00
104,yy,CFO
105,feixue,CTO
3.顯示檔案中包行oldboy的行
[[email protected] oldboy]# sed -n '/oldboy/p' person.txt
101,oldboy,CEO
[[email protected] oldboy]# grep 'oldboy' person.txt
4.顯示檔案中包行oldboy的行到包含104的行。
[[email protected] oldboy]# sed -n '/oldboy/,/^104/p' person.txt
101,oldboy,CEO
102,zhangyao,CTO
103,Alex,C00
104,yy,CFO
[[email protected] oldboy]# grep '^104' person.txt9(grep查詢的時候不能顯示範圍)
104,yy,CFO
[[email protected] oldboy]# sed -n '/^104/p' person.txt (^括號表示更精確一點)
104,yy,CFO
[[email protected] oldboy]# sed -n '/^101/,/^105/p' person.txt
101,oldboy,CEO
102,zhangyao,CTO
103,Alex,C00
104,yy,CFO
105,feixue,CTO
5.查詢多個條件的方法
[[email protected] oldboy]# egrep 'oldboy|yy' person.txt
101,oldboy,CEO
104,yy,CFO
[[email protected] oldboy]# sed -rn '/oldboy|yy/p' person.txt (-rn -r 表示查詢多個條件的內容)
101,oldboy,CEO
104,yy,CFO
[[email protected] oldboy]# sed -n '1p;3p;5p' person.txt ( ;號表示中間不是連續行的內容)
101,oldboy,CEO
103,Alex,C00
105,feixue,CTO
[[email protected] oldboy]# sed -n '/oldboy/p;/yy/p;/feixue/p' person.txt (顯示多行不連續檔案的內容)
101,oldboy,CEO
104,yy,CFO
105,feixue,CTO
sed 查詢的時候都是結合p (preint) 一起來使用
[[email protected] oldboy]# sed '3a yangjuncheng' person.txt (3 表示第三行 a表示增加 3a表示在第三行的後面新增一行)
101,oldboy,CEO
102,zhangyao,CTO
103,Alex,C00
yangjuncheng
104,yy,CFO
105,feixue,CTO
[[email protected] oldboy]# sed '3i aaaaaaaaa' person.txt (i 表示在前面插入內容,如果要寫入檔案是在sed後面加入i,將新增的內容寫到檔案中)
101,oldboy,CEO
102,zhangyao,CTO
aaaaaaaaa
103,Alex,C00
104,yy,CFO
105,feixue,CTO
[[email protected] oldboy]# sed -n '$p' person.txt ($p 表示最後一行)
105,feixue,CTO
[[email protected] oldboy]# sed '$a new,new,new' person.txt
101,oldboy,CEO
102,zhangyao,CTO
103,Alex,C00
104,yy,CFO
105,feixue,CTO
new,new,new
[[email protected] oldboy]# sed '$a new,new,new\nold,old,old' person.txt(檔案的最後一行/n 表示在後面增加多行的內容)
101,oldboy,CEO
102,zhangyao,CTO
103,Alex,C00
104,yy,CFO
105,feixue,CTO
new,new,new
old,old,old
[[email protected] oldboy]# sed -i '$a new,new,new\nold,old,old' person.txt (-i 表示寫入檔案內容中)
[[email protected] oldboy]# cat person.txt
101,oldboy,CEO
102,zhangyao,CTO
103,Alex,C00
104,yy,CFO
105,feixue,CTO
new,new,new
old,old,old
cat >>person.txt <<EOF (在一個檔案最後一行新增多行的方法)
wwwww
cccccccc
dddddddd
QQQQQQ
WWWWW
yang jun cheng
wang bing bing
[[email protected] oldboy]# echo test test >>person.txt (如果在檔案的最後面追加的檔案內容不是很多的時候用echo就行,如果多行就用cat 方法)
[[email protected] oldboy]# cat person.txt
yang jun cheng
wang bing bing
li qang
test test
[[email protected] oldboy]# sed '$d' persion.txt
101,oldboy,CEO
102,zhangyao,CTO
103,Alex,C00
104,yy,CFO
105,feixue,CTO
new,new,new
[[email protected] oldboy]# sed '2,3d' persion.txt (刪除2到3行)
101,oldboy,CEO
104,yy,CFO
105,feixue,CTO
企業案例:不顯示檔案的空行
[[email protected] oldboy]# cat -n persion.txt
1 101,oldboy,CEO
2
3 102,zhangyao,CTO
4
5 103,Alex,C00
6
7 104,yy,CFO
8
9 105,feixue,CTO
[[email protected] oldboy]# cat -A persion.txt (-A 表示不帶空格的行)
101,oldboy,CEO$
$
102,zhangyao,CTO$
$
103,Alex,C00$
$
104,yy,CFO$
$
[[email protected] oldboy]# grep -v '^$' persion.txt (過濾掉空行)
101,oldboy,CEO
102,zhangyao,CTO
103,Alex,C00
104,yy,CFO
105,feixue,CTO
[[email protected] oldboy]# egrep -v '^$' persion.txt(過濾掉空行)
101,oldboy,CEO
102,zhangyao,CTO
103,Alex,C00
104,yy,CFO
105,feixue,CTO
[[email protected] oldboy]# sed '/^$/d' persion.txt (刪除空行的方法,如果要寫入檔案加 -i )
101,oldboy,CEO
102,zhangyao,CTO
103,Alex,C00
104,yy,CFO
105,feixue,CTO
[[email protected] oldboy]# sed -n '/^$/p' persion.txt (顯示檔案的空行)
[[email protected] oldboy]# sed -n '/^$/!p' persion.txt (排除空行方法)
101,oldboy,CEO
102,zhangyao,CTO
103,Alex,C00
104,yy,CFO
105,feixue,CTO
[[email protected] oldboy]# awk '!/^$/' persion.txt (排除空行的方法)
101,oldboy,CEO
102,zhangyao,CTO
103,Alex,C00
104,yy,CFO
105,feixue,CTO
變數引用
[[email protected] oldboy]# x=oldboy
[[email protected] oldboy]# y=oldgirl
[[email protected] oldboy]# sed "s#$x#$y#g" persion.txt (這裡變數的引用必須要用雙引號才行,單引號識別不出來)
101,oldgirl,CEO
102,zhangyao,CTO
103,Alex,C00
104,yy,CFO
105,feixue,CTO
[[email protected] oldboy]# sed "s#oldboy#oldgirl#g" persion.txt
101,oldgirl,CEO
102,zhangyao,CTO
103,Alex,C00
104,yy,CFO
105,feixue,CTO