行編輯器 逐行編輯 主要適用shell 腳本linux
逐行將內容加載到內存進行修改,處理的結果發送到顯示器上。不改變原文件內容 如須要修改原文件內容,須要添加 -i 參數
sed命令格式正則表達式
sed [option選項] scripts腳本 file文件名 sed [option] ‘AddressCommand地址(文件裏的那哪些行進行處理)命令(增 刪改查)’ file
Address(地址)表示方法
option選項:
-n:再也不顯示模式空間的內容
-i:直接修改原文件
-e:同時執行多個匹配操做
能夠同時使用多個條件shell
-f:將多個AddresCommand保存到文件中,每行一個AddresCommand;讀取該文件中的操做同時執行多個操做。bash
-r:表示使用擴展正則表達式app
一、StartLine起始行號,EndLine結束行號
100 一到一百行進行處理
1,$ 從第一行到最後一行進行處理編輯器
二、LineNumber
3 明確指定行號
三、StarLine,+n
5+2 對567這三行進行處理 /root/,2找到帶有root的行以及他的後兩行
四、/正則表達式/
/root/在文件裏找到帶有root的行
/bash$/在文件裏找到以bash結尾行
五、/正則表達式1/,/正則表達式2/
第一次被正則表達式1匹配的行,到正則表達式2匹配的行,取他們中間的全部行。包括他們自己。spa
動做說明:code
p(顯示):對象
打印文件 /etc/fstab 中含有字符串 UUID 的行blog
[root@linuxprobe ~]# sed -n '/UUID/p' /etc/fstab UUID=be2ca297-f7b1-49ac-b4b0-762d54380b1e /boot xfs defaults,uquota 1 2
$p,讀入最後一行時打印模式空間的第一行
打印文件 /etc/fstab 中以 UUID 結尾的行
sed -n '/UUID$/p' /etc/fstab
打印文件 /etc/fstab 中以 UUID 開頭的行
[root@linuxprobe ~]# sed -n '/^UUID/p' /etc/fstab UUID=be2ca297-f7b1-49ac-b4b0-762d54380b1e /boot xfs defaults,uquota 1 2
打印文件 /etc/fstab 含有:號的行
[root@linuxprobe ~]# sed -n '/\:/p' /etc/fstab # Created by anaconda on Sun Dec 1 20:06:57 2019
打印文件 /etc/fstab 中的空行
[root@linuxprobe ~]# sed -n '/^$/p' /etc/fstab
打印文件 /etc/fstab 中 2到6 行
[root@linuxprobe ~]# sed -n '2,6p' /etc/fstab # # /etc/fstab # Created by anaconda on Sun Dec 1 20:06:57 2019 # # Accessible filesystems, by reference, are maintained under '/dev/disk'
打印文件 /etc/fstab 中 除了 2到6 行以外的其餘行
[root@linuxprobe ~]# sed -n '2,6!p' /etc/fstab # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # /dev/mapper/rhel_linuxprobe-root / xfs defaults 1 1 UUID=be2ca297-f7b1-49ac-b4b0-762d54380b1e /boot xfs defaults,uquota 1 2 /dev/mapper/rhel_linuxprobe-swap swap swap defaults 0 0 /dev/cdrom /mnt iso9660 defaults 0 0
a(添加)
找出文件 /etc/fstab 中以 UUID 開頭的行,並在其下一行添加單行字符串(追加到指定行後)
[root@linuxprobe ~]# sed '/^UUID/a\hello sed' /etc/fstab # # /etc/fstab # Created by anaconda on Sun Dec 1 20:06:57 2019 # # 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 # /dev/mapper/rhel_linuxprobe-root / xfs defaults 1 1 UUID=be2ca297-f7b1-49ac-b4b0-762d54380b1e /boot xfs defaults,uquota 1 2 hello sed /dev/mapper/rhel_linuxprobe-swap swap swap defaults 0 0 /dev/cdrom /mnt iso9660 defaults 0 0
找出文件 /etc/fstab 中以 UUID 開頭的行,並在其下一行添加多行字符串
[root@linuxprobe ~]# sed '/^UUID/a\hello sed\nhello sed' /etc/fstab # # /etc/fstab # Created by anaconda on Sun Dec 1 20:06:57 2019 # # 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 # /dev/mapper/rhel_linuxprobe-root / xfs defaults 1 1 UUID=be2ca297-f7b1-49ac-b4b0-762d54380b1e /boot xfs defaults,uquota 1 2 hello sed hello sed /dev/mapper/rhel_linuxprobe-swap swap swap defaults 0 0 /dev/cdrom /mnt iso9660 defaults 0 0
在文件 /etc/fstab 中指定第五行的下一行添加字符串
[root@linuxprobe ~]# sed '5a\hello word' /etc/fstab # # /etc/fstab # Created by anaconda on Sun Dec 1 20:06:57 2019 # hello word # 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 # /dev/mapper/rhel_linuxprobe-root / xfs defaults 1 1 UUID=be2ca297-f7b1-49ac-b4b0-762d54380b1e /boot xfs defaults,uquota 1 2 /dev/mapper/rhel_linuxprobe-swap swap swap defaults 0 0 /dev/cdrom /mnt iso9660 defaults 0 0
在文件 /etc/fstab 中指定第一行、第五行的下一行分別添加字符串
[root@linuxprobe ~]# sed -e '1a\hello word' -e '5a\helloword' /etc/fstab hello word # # /etc/fstab # Created by anaconda on Sun Dec 1 20:06:57 2019 # helloword # 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 # /dev/mapper/rhel_linuxprobe-root / xfs defaults 1 1 UUID=be2ca297-f7b1-49ac-b4b0-762d54380b1e /boot xfs defaults,uquota 1 2 /dev/mapper/rhel_linuxprobe-swap swap swap defaults 0 0 /dev/cdrom /mnt iso9660 defaults 0 0
d(刪除)
帶有0-9的行刪除:
[root@linuxprobe ~]# sed '/[0-9]/d' /etc/fstab # # /etc/fstab # # Accessible filesystems, by reference, are maintained under '/dev/disk' #
顯示文件 /etc/fstab 在 模式空間刪除 含有 UUID 字符串的行
[root@linuxprobe ~]# sed '/UUID/d' /etc/fstab # # /etc/fstab # Created by anaconda on Sun Dec 1 20:06:57 2019 # # 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 # /dev/mapper/rhel_linuxprobe-root / xfs defaults 1 1 /dev/mapper/rhel_linuxprobe-swap swap swap defaults 0 0 /dev/cdrom /mnt iso9660 defaults 0 0
顯示文件 /etc/fstab 在 模式空間刪除除了以 UUID 開頭以外的行後的內容
[root@linuxprobe ~]# sed '/^UUID/!d' /etc/fstab UUID=be2ca297-f7b1-49ac-b4b0-762d54380b1e /boot xfs defaults,uquota 1 2
顯示文件 /etc/fstab 在 模式空間刪除 空行 後的內容
[root@linuxprobe ~]# sed '/^$/d' /etc/fstab # # /etc/fstab # Created by anaconda on Sun Dec 1 20:06:57 2019 # # 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 # /dev/mapper/rhel_linuxprobe-root / xfs defaults 1 1 UUID=be2ca297-f7b1-49ac-b4b0-762d54380b1e /boot xfs defaults,uquota 1 2 /dev/mapper/rhel_linuxprobe-swap swap swap defaults 0 0 /dev/cdrom /mnt iso9660 defaults 0 0
顯示文件 /etc/fstab 在 模式空間 刪除 1到4行後的內容
[root@linuxprobe ~]# sed '1,4d' /etc/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 # /dev/mapper/rhel_linuxprobe-root / xfs defaults 1 1 UUID=be2ca297-f7b1-49ac-b4b0-762d54380b1e /boot xfs defaults,uquota 1 2 /dev/mapper/rhel_linuxprobe-swap swap swap defaults 0 0 /dev/cdrom /mnt iso9660 defaults 0 0
w(寫入)
將文件 /etc/fstab 中以 UUID 開頭的行指定輸出(重定向)到 /tmp/fstab.txt 且不顯示到屏幕
[root@linuxprobe ~]# sed -n '/^UUID/w /tmp/fstab.txt' /etc/fstab
c(更改)
將 /etc/fstab 中含有字符串 UUID 的行替換爲 hello world
[root@linuxprobe ~]# sed '/^UUID/c\hello world' /etc/fstab # # /etc/fstab # Created by anaconda on Sun Dec 1 20:06:57 2019 # # 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 # /dev/mapper/rhel_linuxprobe-root / xfs defaults 1 1 hello world /dev/mapper/rhel_linuxprobe-swap swap swap defaults 0 0 /dev/cdrom /mnt iso9660 defaults 0 0
將 /etc/fstab 中含有字符串以 UUID 開頭的行替換爲兩行內容:hello world 與 hello sed
[root@linuxprobe ~]# sed '/^UUID/c\hello world\nhello sed' /etc/fstab # # /etc/fstab # Created by anaconda on Sun Dec 1 20:06:57 2019 # # 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 # /dev/mapper/rhel_linuxprobe-root / xfs defaults 1 1 hello world hello sed /dev/mapper/rhel_linuxprobe-swap swap swap defaults 0 0 /dev/cdrom /mnt iso9660 defaults 0 0
i(插入)
找出文件 /etc/fstab 中以 UUID 開頭的行,並在其上一行添加單行字符串(插入指定行)
[root@linuxprobe ~]# sed '/^UUID/i\hello world' /etc/fstab # # /etc/fstab # Created by anaconda on Sun Dec 1 20:06:57 2019 # # 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 # /dev/mapper/rhel_linuxprobe-root / xfs defaults 1 1 hello world UUID=be2ca297-f7b1-49ac-b4b0-762d54380b1e /boot xfs defaults,uquota 1 2 /dev/mapper/rhel_linuxprobe-swap swap swap defaults 0 0 /dev/cdrom /mnt iso9660 defaults 0 0
s(替換)
s/old/new/ [修飾符]
old:正則表達式
只替換每一行第一次出現的匹配對象,若是須要所有替換,則須要添加g
s/// 三個斜線能夠替換成任意相同符號 例如 s@@@
把替換的內容寫成空,就能夠刪除全局指定對象
每行替換第一個指定字符串
sed ‘s/要被替代的字符串/新的字符串/’ (斜槓爲定界符)
將每行第一個 / 轉爲 $ (反斜槓爲轉義字符即 \ / 至關於 / )
[root@linuxprobe ~]# sed 's/\//$/' /etc/fstab # # $etc/fstab # Created by anaconda on Sun Dec 1 20:06:57 2019 # # 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 # $dev/mapper/rhel_linuxprobe-root / xfs defaults 1 1 UUID=be2ca297-f7b1-49ac-b4b0-762d54380b1e $boot xfs defaults,uquota 1 2 $dev/mapper/rhel_linuxprobe-swap swap swap defaults 0 0 $dev/cdrom /mnt iso9660 defaults 0 0
全文替換全部指定字符串
sed ‘s/要被取代的字符串/新的字符串/g’ (斜槓爲定界符,g 對模式空間指定狀況的全局更改)
將全部的 / 轉爲 $ (反斜槓爲轉義字符即 \ / 至關於 / )
[root@linuxprobe ~]# sed 's/\//$/g' /etc/fstab # # $etc$fstab # Created by anaconda on Sun Dec 1 20:06:57 2019 # # 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 # $dev$mapper$rhel_linuxprobe-root $ xfs defaults 1 1 UUID=be2ca297-f7b1-49ac-b4b0-762d54380b1e $boot xfs defaults,uquota 1 2 $dev$mapper$rhel_linuxprobe-swap swap swap defaults 0 0 $dev$cdrom $mnt iso9660 defaults 0 0
# 刪除每行前兩個字符
$ sed 's/..//' tmp.txt
#刪除後兩個字母
sed 's/..$//'
顯示類的操做都要加 -n 再也不顯示模式空間的內容
sed -i i直接把結果反饋給原文件 謹慎使用
$P,讀入最後一行時打印模式空間的第一行
$P,讀入最後一行時打印模式空間的第一行
[root@linuxprobe ~]# sed -n '$p' /etc/passwd never:x:1004:1004::/home/never:/bin/bash
打印指定行號(模式空間中的指定行),如以 UUID 開頭的行(行號顯示在其上一行)
[root@linuxprobe ~]# sed '/^UUID/=' /etc/fstab # # /etc/fstab # Created by anaconda on Sun Dec 1 20:06:57 2019 # # 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 # /dev/mapper/rhel_linuxprobe-root / xfs defaults 1 1 10 UUID=be2ca297-f7b1-49ac-b4b0-762d54380b1e /boot xfs defaults,uquota 1 2 /dev/mapper/rhel_linuxprobe-swap swap swap defaults 0 0 /dev/cdrom /mnt iso9660 defaults 0 0
打印文件的總行數
[root@linuxprobe ~]# sed -n '$=' /etc/fstab 12