Linux 文本操作之sed
阿新 • • 發佈:2018-04-27
linux sedsed強大的文本處理工具,其工作流程大概如下
print
常用選項說明
-e<script>或--expression=<script> 以選項中指定的script來處理輸入的文本文件。
-f<script文件>或--file=<script文件> 以選項中指定的script文件來處理輸入的文本文件。
-h或--help 顯示幫助。
-n或--quiet或--silent 僅顯示script處理後的結果。
-V或--version 顯示版本信息。
操作命令:
a :新增, a 的後面可以接字串,而這些字串會在新的一行出現(目前的下一行)~
c :取代, c 的後面可以接字串,這些字串可以取代 n1,n2 之間的行!d :刪除,因為是刪除啊,所以 d 後面通常不接任何咚咚;
i :插入, i 的後面可以接字串,而這些字串會在新的一行出現(目前的上一行);
p :打印,亦即將某個選擇的數據印出。通常 p 會與參數 sed -n 一起運行~
s :取代,可以直接進行取代的工作哩!通常這個 s 的動作可以搭配正規表示法。
1、實驗環境
[root@localhost test]# pwd /root/test [root@localhost test]# ls fstab yum.conf [root@localhost test]# cat -n fstab 1 2 # 3 # /etc/fstab 4 # Created by anaconda on Sun Apr 22 06:26:44 2018 5 # 6 # Accessible filesystems, by reference, are maintained under ‘/dev/disk‘ 7 # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info 8 # 9 UUID=7ceb028a-a8b8-467c-b6d4-36910c06c5ac / xfs defaults 0 0 10 UUID=3d81b92c-abeb-41f5-8de0-b46d3ffbcf4c /boot xfs defaults 0 0 11 UUID=943c7e04-b733-42fe-a1e2-eabf93693f6b swap swap defaults 0 0
花樣刪除:
#刪除1到9行 [root@localhost test]# sed ‘1,9d‘ fstab UUID=3d81b92c-abeb-41f5-8de0-b46d3ffbcf4c /boot xfs defaults 0 0 UUID=943c7e04-b733-42fe-a1e2-eabf93693f6b swap swap defaults 0 0 #-------------------------------------------------------------------------------------------------- #刪除第3行 [root@localhost test]# sed ‘3d‘ fstab # # Created by anaconda on Sun Apr 22 06:26:44 2018 # # Accessible filesystems, by reference, are maintained under ‘/dev/disk‘ # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # UUID=7ceb028a-a8b8-467c-b6d4-36910c06c5ac / xfs defaults 0 0 UUID=3d81b92c-abeb-41f5-8de0-b46d3ffbcf4c /boot xfs defaults 0 0 UUID=943c7e04-b733-42fe-a1e2-eabf93693f6b swap swap defaults 0 0 #-------------------------------------------------------------------------------------------------- #刪除從第3行開始 連續5行的數據 [root@localhost test]# sed ‘3,+5d‘ fstab # UUID=7ceb028a-a8b8-467c-b6d4-36910c06c5ac / xfs defaults 0 0 UUID=3d81b92c-abeb-41f5-8de0-b46d3ffbcf4c /boot xfs defaults 0 0 UUID=943c7e04-b733-42fe-a1e2-eabf93693f6b swap swap defaults 0 0 #-------------------------------------------------------------------------------------------------- #刪除奇數行 [root@localhost test]# sed ‘1~2d‘ fstab # # Created by anaconda on Sun Apr 22 06:26:44 2018 # Accessible filesystems, by reference, are maintained under ‘/dev/disk‘ # UUID=3d81b92c-abeb-41f5-8de0-b46d3ffbcf4c /boot xfs defaults 0 0 #-------------------------------------------------------------------------------------------------- #刪除^UUID=7開始的行 到UUID=9第一次出現的行 如果UUID=9沒有出現,則一直往下刪 [root@localhost test]# sed ‘/^UUID=7/,/^UUID=9/d‘ fstab # # /etc/fstab # Created by anaconda on Sun Apr 22 06:26:44 2018 # # Accessible filesystems, by reference, are maintained under ‘/dev/disk‘ # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # #-------------------------------------------------------------------------------------------------- #刪除第5行開始的行 到UUID=9第一次出現的行 如果UUID=9沒有出現,則一直往下刪 [root@localhost test]# sed ‘5,/^UUID=999/d‘ fstab # # /etc/fstab # Created by anaconda on Sun Apr 22 06:26:44 2018 #-------------------------------------------------------------------------------------------------- #執行兩次操作 [root@localhost test]# sed -e ‘1,3d‘ -e ‘4,6d‘ fstab # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # UUID=7ceb028a-a8b8-467c-b6d4-36910c06c5ac / xfs defaults 0 0 UUID=3d81b92c-abeb-41f5-8de0-b46d3ffbcf4c /boot xfs defaults 0 0 UUID=943c7e04-b733-42fe-a1e2-eabf93693f6b swap swap defaults 0 0 #-------------------------------------------------------------------------------------------------- #執行多次操作 [root@localhost test]# sed ‘1d;2d;3d‘ fstab # Created by anaconda on Sun Apr 22 06:26:44 2018 # # Accessible filesystems, by reference, are maintained under ‘/dev/disk‘ # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # UUID=7ceb028a-a8b8-467c-b6d4-36910c06c5ac / xfs defaults 0 0 UUID=3d81b92c-abeb-41f5-8de0-b46d3ffbcf4c /boot xfs defaults 0 0 UUID=943c7e04-b733-42fe-a1e2-eabf93693f6b swap swap defaults 0 0
#刪除第5行開始的行 到UUID=9第一次出現的行 打印一次,其余正常輸出
[root@localhost test]# sed ‘5,/^UUID=999/p‘ fstab
#
# /etc/fstab
# Created by anaconda on Sun Apr 22 06:26:44 2018
#
#
# Accessible filesystems, by reference, are maintained under ‘/dev/disk‘
# Accessible filesystems, by reference, are maintained under ‘/dev/disk‘
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
#
UUID=7ceb028a-a8b8-467c-b6d4-36910c06c5ac / xfs defaults 0 0
UUID=7ceb028a-a8b8-467c-b6d4-36910c06c5ac / xfs defaults 0 0
UUID=3d81b92c-abeb-41f5-8de0-b46d3ffbcf4c /boot xfs defaults 0 0
UUID=3d81b92c-abeb-41f5-8de0-b46d3ffbcf4c /boot xfs defaults 0 0
UUID=943c7e04-b733-42fe-a1e2-eabf93693f6b swap swap defaults 0 0
UUID=943c7e04-b733-42fe-a1e2-eabf93693f6b swap swap defaults 0 0
#--------------------------------------------------------------------------------------------------
#刪除第5行開始的行 到UUID=9第一次出現的行 看圖理解 打印的行先執行,然後所有的數據用靜默模式處理
[root@localhost test]# sed -n ‘5,/^UUID=999/p‘ fstab
#
# Accessible filesystems, by reference, are maintained under ‘/dev/disk‘
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=7ceb028a-a8b8-467c-b6d4-36910c06c5ac / xfs defaults 0 0
UUID=3d81b92c-abeb-41f5-8de0-b46d3ffbcf4c /boot xfs defaults 0 0
UUID=943c7e04-b733-42fe-a1e2-eabf93693f6b swap swap defaults 0 0
第一行後面插入
[root@localhost test]# sed ‘1a \newline‘ fstab
newline
#
# /etc/fstab
# Created by anaconda on Sun Apr 22 06:26:44 2018
#
# Accessible filesystems, by reference, are maintained under ‘/dev/disk‘
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=7ceb028a-a8b8-467c-b6d4-36910c06c5ac / xfs defaults 0 0
UUID=3d81b92c-abeb-41f5-8de0-b46d3ffbcf4c /boot xfs defaults 0 0
UUID=943c7e04-b733-42fe-a1e2-eabf93693f6b swap swap defaults 0 0
第三行插入
[root@localhost test]# sed ‘3i \newline‘ fstab
newline
#
# /etc/fstab
# Created by anaconda on Sun Apr 22 06:26:44 2018
#
# Accessible filesystems, by reference, are maintained under ‘/dev/disk‘
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=7ceb028a-a8b8-467c-b6d4-36910c06c5ac / xfs defaults 0 0
UUID=3d81b92c-abeb-41f5-8de0-b46d3ffbcf4c /boot xfs defaults 0 0
UUID=943c7e04-b733-42fe-a1e2-eabf93693f6b swap swap defaults 0 0
第三行替換
[root@localhost test]# sed ‘3c \newline‘ fstab
#
newline
# Created by anaconda on Sun Apr 22 06:26:44 2018
#
# Accessible filesystems, by reference, are maintained under ‘/dev/disk‘
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=7ceb028a-a8b8-467c-b6d4-36910c06c5ac / xfs defaults 0 0
UUID=3d81b92c-abeb-41f5-8de0-b46d3ffbcf4c /boot xfs defaults 0 0
UUID=943c7e04-b733-42fe-a1e2-eabf93693f6b swap swap defaults 0 0
把第四行寫入到w.txt文件中
[root@localhost test]# sed ‘4w /root/test/w.txt‘ fstab
#
# /etc/fstab
# Created by anaconda on Sun Apr 22 06:26:44 2018
#
# Accessible filesystems, by reference, are maintained under ‘/dev/disk‘
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=7ceb028a-a8b8-467c-b6d4-36910c06c5ac / xfs defaults 0 0
UUID=3d81b92c-abeb-41f5-8de0-b46d3ffbcf4c /boot xfs defaults 0 0
UUID=943c7e04-b733-42fe-a1e2-eabf93693f6b swap swap defaults 0 0
[root@localhost test]# cat w.txt
# Created by anaconda on Sun Apr 22 06:26:44 2018
讀取文件內容到第六行下面輸出
[root@localhost test]# sed ‘6r /root/test/w.txt‘ fstab
#
# /etc/fstab
# Created by anaconda on Sun Apr 22 06:26:44 2018
#
# Accessible filesystems, by reference, are maintained under ‘/dev/disk‘
# Created by anaconda on Sun Apr 22 06:26:44 2018
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=7ceb028a-a8b8-467c-b6d4-36910c06c5ac / xfs defaults 0 0
UUID=3d81b92c-abeb-41f5-8de0-b46d3ffbcf4c /boot xfs defaults 0 0
UUID=943c7e04-b733-42fe-a1e2-eabf93693f6b swap swap defaults 0 0
為模式匹配到的行打印行號
[root@localhost test]# sed ‘6=‘ fstab
#
# /etc/fstab
# Created by anaconda on Sun Apr 22 06:26:44 2018
#
6
# Accessible filesystems, by reference, are maintained under ‘/dev/disk‘
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=7ceb028a-a8b8-467c-b6d4-36910c06c5ac / xfs defaults 0 0
UUID=3d81b92c-abeb-41f5-8de0-b46d3ffbcf4c /boot xfs defaults 0 0
UUID=943c7e04-b733-42fe-a1e2-eabf93693f6b swap swap defaults 0 0
正則使用
[root@localhost test]# sed ‘1,10s/UUID/uuid/‘ fstab
#
# /etc/fstab
# Created by anaconda on Sun Apr 22 06:26:44 2018
#
# Accessible filesystems, by reference, are maintained under ‘/dev/disk‘
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
uuid=7ceb028a-a8b8-467c-b6d4-36910c06c5ac / xfs defaults 0 0
uuid=3d81b92c-abeb-41f5-8de0-b46d3ffbcf4c /boot xfs defaults 0 0
UUID=943c7e04-b733-42fe-a1e2-eabf93693f6b swap swap defaults 0 0
高級用法:
逆序排序
[root@localhost test]# sed ‘1!G;h;$!d‘ fstab
UUID=943c7e04-b733-42fe-a1e2-eabf93693f6b swap swap defaults 0 0
UUID=3d81b92c-abeb-41f5-8de0-b46d3ffbcf4c /boot xfs defaults 0 0
UUID=7ceb028a-a8b8-467c-b6d4-36910c06c5ac / xfs defaults 0 0
#
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
# Accessible filesystems, by reference, are maintained under ‘/dev/disk‘
#
# Created by anaconda on Sun Apr 22 06:26:44 2018
# /etc/fstab
#
實現過程py分析
mode_space = ‘‘
keep_space = ‘‘
with open(‘test2.txt‘,encoding=‘utf-8‘) as f:
i=0
for line in f:
if i == 0:
mode_space = line
keep_space = mode_space
else:
mode_space = line+keep_space
keep_space = mode_space
i += 1
else:
#最後一行讀完刪除
print(keep_space)
del keep_space
取出最後一行
[root@localhost test]# sed ‘$!d‘ fstab
UUID=943c7e04-b733-42fe-a1e2-eabf93693f6b swap swap defaults 0 0
取出文件後兩行
[root@localhost test]# sed ‘$!N;$!D‘ fstab
UUID=3d81b92c-abeb-41f5-8de0-b46d3ffbcf4c /boot xfs defaults 0 0
UUID=943c7e04-b733-42fe-a1e2-eabf93693f6b swap swap defaults 0 0
顯示偶數行
[root@localhost test]# sed -n ‘n;p‘ fstab
#
# Created by anaconda on Sun Apr 22 06:26:44 2018
# Accessible filesystems, by reference, are maintained under ‘/dev/disk‘
#
UUID=3d81b92c-abeb-41f5-8de0-b46d3ffbcf4c /boot xfs defaults 0 0
顯示奇數行
[root@localhost test]# sed ‘n;d‘ fstab
# /etc/fstab
#
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
UUID=7ceb028a-a8b8-467c-b6d4-36910c06c5ac / xfs defaults 0 0
UUID=943c7e04-b733-42fe-a1e2-eabf93693f6b swap swap defaults 0 0
Linux 文本操作之sed