天天看點

shell文本處理3:sed

Sed 行編輯器

sed(stream editor):

  1. 用來操作純 ASCII 碼的文本
  2. Sed 一次處理一行内容

    處理時,把目前處理的行存儲在臨時緩沖區中,稱之為“模式空間”(pattern space)

  3. 可以指定僅僅處理哪些行,Sed 符合模式條件的處理,不符合條件的不予處理
  4. 處理完成之後把緩沖區的内容送往螢幕
  5. 接着處理下一行,這樣不斷重複,直到檔案末尾

sed指令格式

sed [參數] ‘指令’ file
           

Sed 對字元的處理

p    ##顯示,将某個選擇的資料列印顯示。通常 p 會與參數 sed -n 一起執行
d    ##删除,顯示模式空間删除指定行後的内容,不會對原檔案資料删除
a    ##添加,a 的後面可以接字元串,該字元串會在目前指定行的下一行出現
c    ##更改, c 的後面可以接字元串,該字元串可以取代 n1,n2 之間的行
i    ##插入, i 的後面可以接字元串,該字元串會在目前指定行的上一行出現
           

p

[[email protected] mnt]# cat -n /etc/fstab 
     1	
     2	#
     3	# /etc/fstab
     4	# Created by anaconda on Sun Sep 23 22:41:24 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	/dev/mapper/rhel_foundation63-root /                       xfs     defaults        0 0
    10	UUID=9bbad413-0771-45fc-9ddb-fd2ba6994561 /boot                   xfs     defaults        0 0
    11	UUID=A5EB-573A          /boot/efi               vfat    umask=0077,shortname=winnt 0 0
    12	/dev/mapper/rhel_foundation63-swap swap                    swap    defaults        0 0
[[email protected] mnt]#  sed -n '/\:/p' /etc/fstab 
# Created by anaconda on Sun Sep 23 22:41:24 2018
[[email protected] mnt]# sed -n '/^#/p' /etc/fstab
#
# /etc/fstab
# Created by anaconda on Sun Sep 23 22:41:24 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
#
[[email protected] mnt]# sed -n '/^#/!p' /etc/fstab

/dev/mapper/rhel_foundation63-root /                       xfs     defaults        0 0
UUID=9bbad413-0771-45fc-9ddb-fd2ba6994561 /boot                   xfs     defaults        0 0
UUID=A5EB-573A          /boot/efi               vfat    umask=0077,shortname=winnt 0 0
/dev/mapper/rhel_foundation63-swap swap                    swap    defaults        0 0
[[email protected] mnt]#  sed -n '2,6p' /etc/fstab
#
# /etc/fstab
# Created by anaconda on Sun Sep 23 22:41:24 2018
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
[[email protected] mnt]# sed -n '2p;6p' /etc/fstab  
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
           
shell文本處理3:sed
shell文本處理3:sed
shell文本處理3:sed
shell文本處理3:sed
shell文本處理3:sed
shell文本處理3:sed

d

[[email protected] mnt]# sed '/^UUID/d' fstab 

#
# /etc/fstab
# Created by anaconda on Sun Sep 23 22:41:24 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
#
/dev/mapper/rhel_foundation63-root /                       xfs     defaults        0 0
/dev/mapper/rhel_foundation63-swap swap                    swap    defaults        0 0
[[email protected] mnt]#  sed '/^#/d' fstab 

/dev/mapper/rhel_foundation63-root /                       xfs     defaults        0 0
UUID=9bbad413-0771-45fc-9ddb-fd2ba6994561 /boot                   xfs     defaults        0 0
UUID=A5EB-573A          /boot/efi               vfat    umask=0077,shortname=winnt 0 0
/dev/mapper/rhel_foundation63-swap swap                    swap    defaults        0 0
[[email protected] mnt]# sed '/^$/d' fstab 
#
# /etc/fstab
# Created by anaconda on Sun Sep 23 22:41:24 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
#
/dev/mapper/rhel_foundation63-root /                       xfs     defaults        0 0
UUID=9bbad413-0771-45fc-9ddb-fd2ba6994561 /boot                   xfs     defaults        0 0
UUID=A5EB-573A          /boot/efi               vfat    umask=0077,shortname=winnt 0 0
/dev/mapper/rhel_foundation63-swap swap                    swap    defaults        0 0
[[email protected] mnt]#  sed '1,4d' 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_foundation63-root /                       xfs     defaults        0 0
UUID=9bbad413-0771-45fc-9ddb-fd2ba6994561 /boot                   xfs     defaults        0 0
UUID=A5EB-573A          /boot/efi               vfat    umask=0077,shortname=winnt 0 0
/dev/mapper/rhel_foundation63-swap swap                    swap    defaults        0 0
           
shell文本處理3:sed
shell文本處理3:sed
shell文本處理3:sed
shell文本處理3:sed
shell文本處理3:sed

a

[[email protected] mnt]# cat hello.sh 
hello
[[email protected] mnt]# sed '/hello/aworld' hello.sh 
hello
world
[[email protected] mnt]# sed 's/hello/hello world/g' hello.sh 
hello world
[[email protected] mnt]# 
[[email protected] mnt]# sed 's/hello/hello\nworld/g' hello.sh
hello
world
           
shell文本處理3:sed

c

[[email protected] mnt]# sed '/hello/chello world' hello.sh
hello world
           
shell文本處理3:sed

i

[[email protected] mnt]# sed '/hello/iworld\nwestos' hello.sh 
world
westos
hello
           
shell文本處理3:sed

練習:

将httpd服務的端口80改為8080

#!/bin/bash
yum install httpd -y $> /dev/null
sed -i "/^Listen/cListen $1" /dev/httpd/conf/httpd.conf
echo -e "Port has changed!"
echo "Now,port is $1!"
ststemctl restart httpd
           

~