linux sed流編輯器

1.概述

文本三劍客,grep、sed、awk。今天就簡單記錄sed命令。
sed stream editor 流編輯器
sed 是行編輯器,按行編輯。區別nona,vi(全屏編輯器)

2.sed模式空間

默認不編輯原文件 僅處理模式空間的數據處理,結束後將模式空間中的內容打印至屏幕

3.語法

sed [OPTION]… {script-only-if-no-other-script} [input-file]…

  • [OPTION] 選項,可以多個選項疊加
  • {script-only-if-no-other-script} ‘AddressCommand’ 位置+命令的script,單引號’’
  • [input-file] 文件,可以多個文件

[OPTION]:

  • -n 靜默模式 不在顯示模式空間中的內容
  • -i 直接修改源文件
  • -e script -e script 可以同時執行多個腳本(‘AddressCommand’)
  • -f script-file script 可以寫成腳本文件後運行
  • -r 使用擴展的正則表達式

Address

  • 1.startLine,Endline
    1,100 第1行到第100行
    $ 最後一行
  • 2./RegExp/
    /^root/ 表示匹配到 行首以root開頭的行
  • 3./RegExp1/,/RegExp2/
    第一次被模式1 匹配到的行開始到模式2匹配到的行結束
    當然也可以 /RegExp1/,$ 第一次被模式1匹配到的行到文件尾
  • 4.LineNumber
    指定行
  • 5.StartLinne, +N
    從指定行開始,向後N行 總共n+1 行

Command

  • d:刪除符合條件的行
  • p:顯示符合條件的行 ,常配合-n 選項 靜默模式
  • a “string”: 在指定的行後面追加新行,內容爲string
  • r FILE:將指定的文件與內容添加到符合條件的行處
  • w FILE 將指定範圍內的內容保存至指定的文件
  • s/Pattern/string/  查找並替換 只替換每一行中第一次被模式匹配到的串

以上命令一般 s/Pattern/string/ 命令 使用較多; a "string" 個人認爲也有使用場景

舉個栗子

/etc/fdtab 文件內容

etc$cat /etc/fstab
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
# / was on /dev/sda1 during installation
UUID=87a6d5b6-b007-4c99-9ea2-b1ec09b27112 /               ext4    errors=remount-ro 0       1
# swap was on /dev/sda5 during installation
UUID=d1b31722-7161-4293-ac59-5944c75f050e none            swap    sw              0       0
/dev/fd0        /media/floppy0  auto    rw,user,noauto,exec,utf8 0       0
etc$

d:刪除符合條件的行

1.sed ‘3,$d’ /etc/fstab 刪除第三行到行尾,模式空間內剩下第1、2行

etc$sed '3,$d' /etc/fstab
# /etc/fstab: static file system information.
#
etc$

2.sed ‘/dev/d’ /etc/fstab 刪除/oot/匹配到的行

etc$sed '/dev/d' /etc/fstab
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
UUID=87a6d5b6-b007-4c99-9ea2-b1ec09b27112 /               ext4    errors=remount-ro 0       1
UUID=d1b31722-7161-4293-ac59-5944c75f050e none            swap    sw              0       0
etc$

3.sed ‘1,+2d’ /etc/fstab 刪除1、2、3行

etc$sed '1,+2d' /etc/fstab
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
# / was on /dev/sda1 during installation
UUID=87a6d5b6-b007-4c99-9ea2-b1ec09b27112 /               ext4    errors=remount-ro 0       1
# swap was on /dev/sda5 during installation
UUID=d1b31722-7161-4293-ac59-5944c75f050e none            swap    sw              0       0
/dev/fd0        /media/floppy0  auto    rw,user,noauto,exec,utf8 0       0
etc$

p:顯示符合條件的行 ,常配合-n 選項 靜默模式
1.sed -n ‘/^u/p’ /etc/fstab 顯示行首以u開頭的行並顯示
ps:若不加-n 會打印原來模式空間中的內容+匹配到的行

etc$sed  -n '/^U/p' /etc/fstab
UUID=87a6d5b6-b007-4c99-9ea2-b1ec09b27112 /               ext4    errors=remount-ro 0       1
UUID=d1b31722-7161-4293-ac59-5944c75f050e none            swap    sw              0       0
etc$

a “string”: 在指定的行後面追加新行,內容爲string
i “string”: 前面
\n用於換行
1.sed ‘/^U/a “hello world”’ /etc/fstab 在匹配到後面顯示

etc$sed '/^U/a \"hello world"' /etc/fstab
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
# / was on /dev/sda1 during installation
UUID=87a6d5b6-b007-4c99-9ea2-b1ec09b27112 /               ext4    errors=remount-ro 0       1
"hello world"
# swap was on /dev/sda5 during installation
UUID=d1b31722-7161-4293-ac59-5944c75f050e none            swap    sw              0       0
"hello world"
/dev/fd0        /media/floppy0  auto    rw,user,noauto,exec,utf8 0       0
etc$

2.sed -n -e ‘/^U/p’ -e ‘/^U/a “hello world”’ /etc/fstab

etc$sed -n -e '/^U/p' -e '/^U/a \"hello world"' /etc/fstab
UUID=87a6d5b6-b007-4c99-9ea2-b1ec09b27112 /               ext4    errors=remount-ro 0       1
"hello world"
UUID=d1b31722-7161-4293-ac59-5944c75f050e none            swap    sw              0       0
"hello world"
etc$

r FILE:將指定的文件與內容添加到符合條件的行處
1.sed ‘2r /etc/issue’ /etc/fstab

etc$sed '2r /etc/issue' /etc/fstab
# /etc/fstab: static file system information.
#
Ubuntu 16.04.3 LTS \n \l

# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
# / was on /dev/sda1 during installation
UUID=87a6d5b6-b007-4c99-9ea2-b1ec09b27112 /               ext4    errors=remount-ro 0       1
# swap was on /dev/sda5 during installation
UUID=d1b31722-7161-4293-ac59-5944c75f050e none            swap    sw              0       0
/dev/fd0        /media/floppy0  auto    rw,user,noauto,exec,utf8 0       0
etc$

w FILE 將指定範圍內的內容保存至指定的文件
sed -n ‘/oot/w /tmp/oot.txt’ /etc/fstab

s/Pattern/string/  查找並替換 只替換每一行中第一次被模式匹配到的串

 

注:默認替換每行中第一次被模式匹配的串
修飾符

  • g 全局替換
  • i 忽略大小寫


sed ‘1,$s/dev/DEV/’ /etc/fstab
sed ‘s/dev/DEV/’ /etc/fstab  若’Address’ 沒有則表示全文

etc$sed 's/dev/DEV/' /etc/fstab
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# DEVice; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
# / was on /DEV/sda1 during installation
UUID=87a6d5b6-b007-4c99-9ea2-b1ec09b27112 /               ext4    errors=remount-ro 0       1
# swap was on /DEV/sda5 during installation
UUID=d1b31722-7161-4293-ac59-5944c75f050e none            swap    sw              0       0
/DEV/fd0        /media/floppy0  auto    rw,user,noauto,exec,utf8 0       0
etc$

sed ‘s/^//#/’ /etc/fstab ^/ 行首的/ (’/’ 轉義) 替換成 #

etc$sed 's/^\//#/' /etc/fstab
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
# / was on /dev/sda1 during installation
UUID=87a6d5b6-b007-4c99-9ea2-b1ec09b27112 /               ext4    errors=remount-ro 0       1
# swap was on /dev/sda5 during installation
UUID=d1b31722-7161-4293-ac59-5944c75f050e none            swap    sw              0       0
#dev/fd0        /media/floppy0  auto    rw,user,noauto,exec,utf8 0       0
etc$

 

sed 練習

1.刪除/etc/grub.conf 文件中行首的空白符
2.替換/etc/inittab文件中的"id:3:initdefault:"一行中的數字爲5
3.刪除/etc/inittab文件中的空白行
4.刪除/etc/inittab文件中的開頭的#號
5.刪除/etc/inittab文件中的開頭的#號及後面空白字符,但要求#後面必須有空白字符
6.刪除某文件中以空白字符後面跟#類的行中的開頭的空白字符及#

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章