sed命令語法格式
sed [參數] [條件/指令] [文件名稱]
參數 | 功能 |
---|---|
-n | 取消默認輸出 |
-i | 修改文件內容 |
-e | 執行多個指令 |
-r | 識別擴展正則符號 |
指令 | 功能 |
---|---|
p | 輸出 |
d | 刪除 |
s | 替換 |
g | 匹配文件內容中的所有信息 |
i | 在當前行上一行插入信息 |
a | 在當前行下一行插入信息 |
c | 替換正行信息 |
增加測試文本
[root@sed ~]# cat << EOF > test.sh
> 1001,AAAA,CEO
> 1002,BBBB,CFO
> 1003,CCCC,CTO
> 1004,DDDD,COO
> 1005,EEEE,CIO
> EOF
[root@sed ~]# cat test.sh
1001,AAAA,CEO
1002,BBBB,CFO
1003,CCCC,CTO
1004,DDDD,COO
1005,EEEE,CIO
一、sed - 查詢
1、查看文本所有內容
[root@sed ~]# sed '' test.sh
1001,AAAA,CEO
1002,BBBB,CFO
1003,CCCC,CTO
1004,DDDD,COO
1005,EEEE,CIO
2、查看文本第二行內容
[root@sed ~]# sed -n '2p' test.sh
1002,BBBB,CFO
3、如果不加-n
參數的話,則顯示文本的所有內容,在這裏可以將-n
理解爲消除多餘輸出
[root@sed ~]# sed '2p' test.sh
1001,AAAA,CEO
1002,BBBB,CFO
1002,BBBB,CFO
1003,CCCC,CTO
1004,DDDD,COO
1005,EEEE,CIO
4、查看文本2-4
行的內容(連續)
[root@sed ~]# sed -n '2,4p' test.sh
1002,BBBB,CFO
1003,CCCC,CTO
1004,DDDD,COO
5、查看文本指定行(第2和第4行
)內容(不連續)
[root@sed ~]# sed -n '2p;4p' test.sh
1002,BBBB,CFO
1004,DDDD,COO
6、使用-e
參數將指令分開執行
[root@sed ~]# sed -ne '2p' -e '4p' test.sh
1002,BBBB,CFO
1004,DDDD,COO
7、使用!
排除指定單行
[root@sed ~]# sed -n '2!p' test.sh
1001,AAAA,CEO
1003,CCCC,CTO
1004,DDDD,COO
1005,EEEE,CIO
8、使用!
排除2-4行(連續)
[root@sed ~]# sed -n '2,4!p' test.sh
1001,AAAA,CEO
1005,EEEE,CIO
9、查詢文本中帶有00
字段的內容
[root@sed ~]# sed -n '/00/p' test.sh
1001,AAAA,CEO
1002,BBBB,CFO
1003,CCCC,CTO
1004,DDDD,COO
1005,EEEE,CIO
10、查詢文本中CEO
到CTO
中的所有字段
[root@sed ~]# sed -n '/CEO/,/CTO/p' test.sh
1001,AAAA,CEO
1002,BBBB,CFO
1003,CCCC,CTO
11、查詢以1003
開頭的字段
[root@sed ~]# sed -n '/^1003/p' test.sh
1003,CCCC,CTO
12、查詢文本最後一行內容
[root@sed ~]# sed -n '$p' test.sh
1005,EEEE,CIO
二、sed - 增加
1、新增內容到文本第二行下
中需要加一個參數-i
,但不可與-n
連用,否則會將文件內容清空
[root@sed ~]# sed -i '2a 1111111111' test.sh
[root@sed ~]# sed '' test.sh
1001,AAAA,CEO
1002,BBBB,CFO
1111111111
1003,CCCC,CTO
1004,DDDD,COO
1005,EEEE,CIO
2、新增內容到文本第二行上
中需要加一個參數-i
,但不可與-n
連用,否則會將文件內容清空
[root@sed ~]# sed -i '2i 222222222222' test.sh
[root@sed ~]# sed '' test.sh
1001,AAAA,CEO
222222222222
1002,BBBB,CFO
1003,CCCC,CTO
1004,DDDD,COO
1005,EEEE,CIO
3、向文本最後追加內容
[root@sed ~]# sed -i '$a 333333333333' test.sh
[root@sed ~]# sed '' test.sh
1001,AAAA,CEO
1002,BBBB,CFO
1003,CCCC,CTO
1004,DDDD,COO
1005,EEEE,CIO
333333333333
4、向文本中追加多行內容
[root@sed ~]# sed -i '3a 4444444\n5555555' test.sh
[root@sed ~]# sed '' test.sh
1001,AAAA,CEO
1002,BBBB,CFO
1003,CCCC,CTO
4444444
5555555
1004,DDDD,COO
1005,EEEE,CIO
5、向文本指定行添加多行內容
[root@sed ~]# sed -e '2i 1111111' -e '2a 2222222' test.sh
1001,AAAA,CEO
1111111
1002,BBBB,CFO
2222222
1003,CCCC,CTO
1004,DDDD,COO
1005,EEEE,CIO
6、向每行行首添加#
字符
[root@sed ~]# sed -i 's/^/#/g' test.sh
[root@sed ~]# sed '' test.sh
#1001,AAAA,CEO
#1002,BBBB,CFO
#1003,CCCC,CTO
#1004,DDDD,COO
#1005,EEEE,CIO
三、sed - 修改
1、修改指定內容
[root@sed ~]# sed -i 's#CTO#CCCCCCCCCCCC#g' test.sh
[root@sed ~]# sed '' test.sh
1001,AAAA,CEO
1002,BBBB,CFO
1003,CCCC,CCCCCCCCCCCC
1004,DDDD,COO
1005,EEEE,CIO
2、修改文本中的內容O
修改爲W
[root@sed ~]# sed -i 's#O#W#' test.sh
[root@sed ~]# sed '' test.sh
1001,AAAA,CEW
1002,BBBB,CFW
1003,CCCC,CTW
1004,DDDD,CWW
1005,EEEE,CIW
3、匹配到某行的單個內容,並修改整行內容:CIO
字段行修改爲6666666
[root@sed ~]# sed -i '/CIO/c 66666666' test.sh
[root@sed ~]# sed '' test.sh
1001,AAAA,CEO
1002,BBBB,CFO
1003,CCCC,CTO
1004,DDDD,COW
66666666
4、修改指定行的正行內容
[root@sed ~]# sed -i '1c AAAAAAAA' test.sh
[root@sed ~]# sed '' test.sh
AAAAAAAA
1002,BBBB,CFO
1003,CCCC,CTO
1004,DDDD,COO
1005,EEEE,CIO
四、sed - 刪除
1、刪除空行
[root@sed ~]# sed '' test.sh
1001,AAAA,CEO
1002,BBBB,CFO
1003,CCCC,CTO
1004,DDDD,COW
[root@sed ~]# sed -i '/^$/d' test.sh
[root@sed ~]# sed '' test.sh
1001,AAAA,CEO
1002,BBBB,CFO
1003,CCCC,CTO
1004,DDDD,COW
2、刪除#
字符
[root@sed ~]# sed '' test.sh
1001,AAAA,CEO
1002,BBBB,CFO
1003,CCCC,CTO
1004,DDDD,COO
1005,EEEE,CIO
######
[root@sed ~]# sed -i '/#/d' test.sh
[root@sed ~]# sed '' test.sh
1001,AAAA,CEO
1002,BBBB,CFO
1003,CCCC,CTO
1004,DDDD,COO
1005,EEEE,CIO
3、刪除指定行內容
[root@sed ~]# sed '' test.sh
1001,AAAA,CEO
1002,BBBB,CFO
1003,CCCC,CTO
1004,DDDD,COW
66666666
[root@sed ~]# sed -i '5d' test.sh
[root@sed ~]# sed '' test.sh
1001,AAAA,CEO
1002,BBBB,CFO
1003,CCCC,CTO
1004,DDDD,COW
4、刪除1-3
行內容(連續)
[root@sed ~]# sed '' test.sh
1001,AAAA,CEO
1002,BBBB,CFO
1003,CCCC,CTO
1004,DDDD,COW
[root@sed ~]# sed -i '1,3d' test.sh
[root@sed ~]# sed '' test.sh
1004,DDDD,COW
5、刪除指定多行內容
方法一:
[root@sed ~]# sed '' test.sh
1001,AAAA,CEO
1002,BBBB,CFO
1003,CCCC,CTO
1004,DDDD,COO
1005,EEEE,CIO
[root@sed ~]# sed -i '1d;3d' test.sh
[root@sed ~]# sed '' test.sh
1002,BBBB,CFO
1004,DDDD,COO
1005,EEEE,CIO
方法二:
[root@sed ~]# sed '' test.sh
1002,BBBB,CFO
1004,DDDD,COO
1005,EEEE,CIO
[root@sed ~]# sed -e '1d' -e '2d' test.sh
1005,EEEE,CIO
6、刪除每行行首#
字符
[root@sed ~]# sed '' test.sh
#1001,AAAA,CEO
#1002,BBBB,CFO
#1003,CCCC,CTO
#1004,DDDD,COO
#1005,EEEE,CIO
[root@sed ~]# sed -i 's/^#//' test.sh
[root@sed ~]# sed '' test.sh
1001,AAAA,CEO
1002,BBBB,CFO
1003,CCCC,CTO
1004,DDDD,COO
1005,EEEE,CIO
六、sed - 排除
1、排除第一行內容
[root@sed ~]# sed -n '1!p' test.sh
1002,BBBB,CFO
1003,CCCC,CTO
1004,DDDD,COO
1005,EEEE,CIO
2、排除連續多行內容
[root@sed ~]# sed -n '1,3!p' test.sh
1004,DDDD,COO
1005,EEEE,CIO
練習
1、批量修改文件的擴展名,將villian.txt
修改爲villian.png
[root@sed zwl]# touch villian{1..5}.txt
[root@sed zwl]# ll
總用量 0
-rw-r--r--. 1 root root 0 3月 4 13:52 villian1.txt
-rw-r--r--. 1 root root 0 3月 4 13:52 villian2.txt
-rw-r--r--. 1 root root 0 3月 4 13:52 villian3.txt
-rw-r--r--. 1 root root 0 3月 4 13:52 villian4.txt
-rw-r--r--. 1 root root 0 3月 4 13:52 villian5.txt
[root@sed zwl]# ls villian*.txt|sed -r 's#(.*)txt#mv & \1png#g'|bash
[root@sed zwl]# ll
總用量 0
-rw-r--r--. 1 root root 0 3月 4 13:52 villian1.png
-rw-r--r--. 1 root root 0 3月 4 13:52 villian2.png
-rw-r--r--. 1 root root 0 3月 4 13:52 villian3.png
-rw-r--r--. 1 root root 0 3月 4 13:52 villian4.png
-rw-r--r--. 1 root root 0 3月 4 13:52 villian5.png
PS:替換命令 s/A/B/g
,B中的 \1
指的就是正則A中的第一個小括號裏匹配到的內容(.*)
,相當於mv \1 png
PS:專業命令:rename 原文件的格式 修改後文件的格式 原文件的文件名
------rename .txt .png villian*.txt