1. 程式人生 > 其它 >shell對檔案的操作(sed)

shell對檔案的操作(sed)

 

 

一、簡介

 

  在shell指令碼編寫中,時常會用到對檔案的相關操作,比如增加內容,修改內容,刪除部分內容,檢視部分內容等,但是上述舉例的這些操作一般都是需要在文字編輯器中才能操作,常用的文字編輯器如:gedit、vim、nano等又是互動式文字編輯器,指令碼無法自己獨立完成,必須有人蔘與才可以完成。如果這樣的話又違背了我們編寫指令碼的意願(全部由機器來完成,減少人的工作壓力,提升工作效率)。emm…如何才能讓這些操作全部指令碼自己就搞定,而不需要人的參與,而且又能按照我們的指令碼預案來完成呢?

為了解決上述問題,linux為大家提供了一些命令,比如Perl、sed等命令,著重介紹一下sed命令。

 

 

二、sed命令

 

1、sed介紹

         sed是linux中提供的一個外部命令,它是一個行(流)編輯器,非互動式的對檔案內容進行增刪改查的操作,使用者只能在命令列輸入編輯命令、指定檔名,然後在螢幕上檢視輸出。它和文字編輯器有本質的區別。

區別是: 文字編輯器: 編輯物件是檔案 行編輯器:編輯物件是檔案中的行

也就是前者一次處理一個文字,而後者是一次處理一個文字中的一行。這個是我們應該弄清楚且必須牢記的,否者可能無法理解sed的執行原理和使用精髓。

 

 

sed資料處理原理

 

 

 

 

2、sed語法

 

sed 命令語法:

sed [options] ‘{command}[flags]’ [filename]

命令選項:

-e script 將指令碼中指定的命令新增到處理輸入時執行的命令中  多條件,一行中要有多個操作
-f script 將檔案中指定的命令新增到處理輸入時執行的命令中
-n        抑制自動輸出
-i        編輯檔案內容
-i.bak    修改時同時建立.bak備份檔案。
-r        使用擴充套件的正則表示式
!         取反 (跟在模式條件後與shell有所區別)

command   對檔案幹什麼

sed常用內部命令 a 在匹配後面新增 i 在匹配前面新增 d 刪除 s 查詢替換 字串 c 更改 y 轉換 N D P p 列印 #flags 數字 表示新文字替換的模式 g: 表示用新文字替換現有文字的全部例項 p: 表示列印原始的內容 w filename: 將替換的結果寫入檔案

 

3,演示示例

 

先準備一個檔案

 

(1)command演示

 

[root@CentOs shell]# vim data

1 the quick brown fox jumps over the lazy dog.

2 the quick brown fox jumps over the lazy dog.

3 the quick brown fox jumps over the lazy dog.

4 the quick brown fox jumps over the lazy dog.

5 the quick brown fox jumps over the lazy dog.

 

a新增後面命令:

[root@CentOs shell]# sed 'a\hello,world' data
1 the quick brown fox jumps over the lazy dog.
hello,world
2 the quick brown fox jumps over the lazy dog.
hello,world
3 the quick brown fox jumps over the lazy dog.
hello,world
4 the quick brown fox jumps over the lazy dog.
hello,world
5 the quick brown fox jumps over the lazy dog.
hello,world
[root@CentOs shell]#

 

[root@CentOs shell]# sed '3a\hello,world' data (對第三行追加)
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy dog.
hello,world
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.
[root@CentOs shell]#

 

 

[root@CentOs shell]# sed '2,4a\hello,world' data
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
hello,world
3 the quick brown fox jumps over the lazy dog.
hello,world
4 the quick brown fox jumps over the lazy dog.
hello,world
5 the quick brown fox jumps over the lazy dog.
[root@CentOs shell]#

[root@CentOs shell]# sed '/3 the/a\hello,world' data (“//”是開啟匹配模式,這個生產環境中常用!!!)
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy dog.
hello,world
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.
[root@CentOs shell]#

 

i插入前面命令:

[root@CentOs shell]# sed '/3 the/i\hello,world' data
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
hello,world
3 the quick brown fox jumps over the lazy dog.
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.
[root@CentOs shell]#

 

d刪除命令:

[root@CentOs shell]# sed '/3 the/d' data
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.


[root@CentOs shell]# cp /usr/local/nginx/conf/nginx.conf .

[root@CentOs shell]# sed -r '/^#/d' nginx.conf  (刪除以#開頭的行)

[root@CentOs shell]# sed -r '/(^#|#|^$)/d' nginx.conf  (刪除以#開頭或者包含#或者空行的行)
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
[root@CentOs shell]#


 

s替換命令:

 

[root@CentOs shell]# cat data
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy dog.
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.
[root@CentOs shell]# sed 's/dog/cat/' data (把dog替換成cat)
1 the quick brown fox jumps over the lazy cat.
2 the quick brown fox jumps over the lazy cat.
3 the quick brown fox jumps over the lazy cat.
4 the quick brown fox jumps over the lazy cat.
5 the quick brown fox jumps over the lazy cat.
[root@CentOs shell]#

[root@CentOs shell]# sed '/3 the/s/dog/cat/' data
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy cat.
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.
[root@CentOs shell]#

 

c更改命令

 

[root@CentOs shell]# sed '/3 the/c\hello world' data
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
hello world
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.
[root@CentOs shell]#

 

[root@CentOs shell]# sed '2,4c\hello world' data(這個有點特殊,把2-4行刪除,再新增)
1 the quick brown fox jumps over the lazy dog.
hello world
5 the quick brown fox jumps over the lazy dog.
[root@CentOs shell]#

 

y轉換命令:

 

[root@CentOs shell]# sed 'y/abcdefg/ABCDEFG/' data
1 thE quiCk Brown Fox jumps ovEr thE lAzy DoG.
2 thE quiCk Brown Fox jumps ovEr thE lAzy DoG.
3 thE quiCk Brown Fox jumps ovEr thE lAzy DoG.
4 thE quiCk Brown Fox jumps ovEr thE lAzy DoG.
5 thE quiCk Brown Fox jumps ovEr thE lAzy DoG.
[root@CentOs shell]#

 

 

p列印命令

 

[root@CentOs shell]# sed 'p' data (出現兩行,是因為把記憶體和檔案都列印了出來!!!)
1 the quick brown fox jumps over the lazy dog.
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy dog.
4 the quick brown fox jumps over the lazy dog.
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.
[root@CentOs shell]#

[root@CentOs shell]# sed '/3 the/p' data
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy dog.
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.
[root@CentOs shell]#

 

(2)flags演示

 

數字:

[root@CentOs shell]# cat data
1 the quick brown fox jumps over the lazy dog.dog
2 the quick brown fox jumps over the lazy dog.dog
3 the quick brown fox jumps over the lazy dog.dog
4 the quick brown fox jumps over the lazy dog.dog
5 the quick brown fox jumps over the lazy dog.dog
[root@CentOs shell]# sed 's/dog/cat/' data
1 the quick brown fox jumps over the lazy cat.dog
2 the quick brown fox jumps over the lazy cat.dog
3 the quick brown fox jumps over the lazy cat.dog
4 the quick brown fox jumps over the lazy cat.dog
5 the quick brown fox jumps over the lazy cat.dog
[root@CentOs shell]# sed 's/dog/cat/2' data
1 the quick brown fox jumps over the lazy dog.cat
2 the quick brown fox jumps over the lazy dog.cat
3 the quick brown fox jumps over the lazy dog.cat
4 the quick brown fox jumps over the lazy dog.cat
5 the quick brown fox jumps over the lazy dog.cat
[root@CentOs shell]#

 

g:全部替換

 

[root@CentOs shell]# sed 's/dog/cat/g' data
1 the quick brown fox jumps over the lazy cat.cat
2 the quick brown fox jumps over the lazy cat.cat
3 the quick brown fox jumps over the lazy cat.cat
4 the quick brown fox jumps over the lazy cat.cat
5 the quick brown fox jumps over the lazy cat.cat
[root@CentOs shell]#

p:列印輸出

 

[root@CentOs shell]# sed '3s/dog/cat/p' data
1 the quick brown fox jumps over the lazy dog.dog
2 the quick brown fox jumps over the lazy dog.dog
3 the quick brown fox jumps over the lazy cat.dog
3 the quick brown fox jumps over the lazy cat.dog
4 the quick brown fox jumps over the lazy dog.dog
5 the quick brown fox jumps over the lazy dog.dog
[root@CentOs shell]#

 

w filename:將替換的結果寫入檔案中

 

[root@CentOs shell]# sed '3s/dog/cat/w newfile data
1 the quick brown fox jumps over the lazy dog.dog
2 the quick brown fox jumps over the lazy dog.dog
3 the quick brown fox jumps over the lazy cat.dog
4 the quick brown fox jumps over the lazy dog.dog
5 the quick brown fox jumps over the lazy dog.dog
[root@CentOs shell]# ls
data        ip.txt   nginx.conf           ShellTest.sh  zzku.sh
fenjie.txt  newfile  NginxTestfailing.sh  test01.sh
[root@CentOs shell]# cat newfile
3 the quick brown fox jumps over the lazy cat.dog
[root@CentOs shell]#



 

(3)命令選項

 

-n:只輸入自己想要的那部分

[root@CentOs shell]# sed -n '3s/dog/cat/p' data
3 the quick brown fox jumps over the lazy cat.dog
[root@CentOs shell]#

-e:多條件,一行中要有多個操作

 

[root@CentOs shell]# sed -e 's/brown/grey/;s/dog/cat/' data
1 the quick grey fox jumps over the lazy cat.dog
2 the quick grey fox jumps over the lazy cat.dog
3 the quick grey fox jumps over the lazy cat.dog
4 the quick grey fox jumps over the lazy cat.dog
5 the quick grey fox jumps over the lazy cat.dog
[root@CentOs shell]#

 

-f:將檔案中的命令新增到處理輸入時執行的命令中

 

[root@CentOs shell]# vim file
[root@CentOs shell]# cat file
s/brown/grey/
s/dog/cat/
[root@CentOs shell]# sed -f file data
1 the quick grey fox jumps over the lazy cat.dog
2 the quick grey fox jumps over the lazy cat.dog
3 the quick grey fox jumps over the lazy cat.dog
4 the quick grey fox jumps over the lazy cat.dog
5 the quick grey fox jumps over the lazy cat.dog
[root@CentOs shell]#

 

-i:修改原始檔(不可逆)

 

[root@CentOs shell]# sed -i 's/dog/cat/g' data
[root@CentOs shell]# cat data
1 the quick brown fox jumps over the lazy cat.cat
2 the quick brown fox jumps over the lazy cat.cat
3 the quick brown fox jumps over the lazy cat.cat
4 the quick brown fox jumps over the lazy cat.cat
5 the quick brown fox jumps over the lazy cat.cat
[root@CentOs shell]#

 

[root@CentOs shell]# sed -i.bak 's/cat/dog/g' data(使用-i時,可以備份一個檔案)

[root@CentOs shell]# ls
data      fenjie.txt  ip.txt      NginxTestfailing.sh  test01.sh
data.bak  file        nginx.conf  ShellTest.sh         zzku.sh
[root@CentOs shell]# cat data.bak (原始檔的備份還是沒有變化)
1 the quick brown fox jumps over the lazy cat.cat
2 the quick brown fox jumps over the lazy cat.cat
3 the quick brown fox jumps over the lazy cat.cat
4 the quick brown fox jumps over the lazy cat.cat
5 the quick brown fox jumps over the lazy cat.cat
[root@CentOs shell]# cat data(原始檔已被修改)
1 the quick brown fox jumps over the lazy dog.dog
2 the quick brown fox jumps over the lazy dog.dog
3 the quick brown fox jumps over the lazy dog.dog
4 the quick brown fox jumps over the lazy dog.dog
5 the quick brown fox jumps over the lazy dog.dog
[root@CentOs shell]#

 

-r:使用正則表示式

[root@CentOs shell]# sed -r -n '/^(root)(.*)(bash)$/p' /etc/passwd(列印以root開頭、中間任意、結尾以bash結束的行)
root:x:0:0:root:/root:/bin/bash
[root@CentOs shell]#

 

!:取反

 

 

我們也不一定非得使用檔案輸出。以管道為例:

[root@CentOs shell]# echo "Tom is cool"|sed 's/Tom is/I am/'
I am cool
[root@CentOs shell]#

 

 

三、sed的一些小技巧

$=:統計行號

 

[root@CentOs shell]# sed -n '$=' data
5
[root@CentOs shell]#

=:給每一行加行號

 

[root@CentOs shell]# sed '=' data
1
1 the quick brown fox jumps over the lazy dog.dog
2
2 the quick brown fox jumps over the lazy dog.dog
3
3 the quick brown fox jumps over the lazy dog.dog
4
4 the quick brown fox jumps over the lazy dog.dog
5
5 the quick brown fox jumps over the lazy dog.dog
[root@CentOs shell]#

 

四、寫一個搭建ftp服務的指令碼

 

要求如下:

1)不支援本地使用者登入 local_enable=NO
2) 匿名使用者可以上傳 新建 刪除 anon_upload_enable=YES anon_mkdir_write_enable=YES
3) 匿名使用者限速500KBps anon_max_rate=500000

 

僅供參考:
#!/bin/bash
ipaddr=`ifconfig eth0|sed -n '2p'|sed -e 's/.*inet addr:\(.*\) Bcast.*/\1/g'`
iptail=`echo $ipaddr|cut -d'.' -f4`
ipremote=192.168.1.10
#修改主機名
hostname server$iptail.zutuanxue.com
sed -i "/HOSTNAME/cHOSTNAME=server$iptail.zutuanxue.com" /etc/sysconfig/network
echo "$ipaddr server$iptail.zutuanxue.cc" >>/etc/hosts
#關閉防火牆和selinux
service iptables stop
setenforce 0 >/dev/null 2>&1
sed -i '/^SELINUX=/cSELINUX=disabled' /etc/selinux/config
#配置yum源(一般是內網源)
#test network
ping -c 1 $ipremote > /dev/null 2>&1
if [ $? -ne 0 ];then
	echo "你的網路不通,請先檢查你的網路"
	exit 1
else
	echo "網路ok."
fi
cat > /etc/yum.repos.d/server.repo << end
[server]
name=server
baseurl=ftp://$ipremote
enabled=1
gpgcheck=0
end

#安裝軟體
read -p "請輸入需要安裝的軟體,多個用空格隔開:" soft
yum -y install $soft &>/dev/null

#備份配置檔案
conf=/etc/vsftpd/vsftpd.conf
\cp $conf $conf.default
#根據需求修改配置檔案
sed -ir '/^#|^$/d' $conf
sed -i '/local_enable/c\local_enable=NO' $conf
sed -i '$a anon_upload_enable=YES' $conf
sed -i '$a anon_mkdir_write_enable=YES' $conf
sed -i '$a anon_other_write_enable=YES' $conf
sed -i '$a anon_max_rate=512000' $conf
#啟動服務
service vsftpd restart &>/dev/null && echo"vsftpd服務啟動成功"

#測試驗證
chmod 777 /var/ftp/pub
cp /etc/hosts /var/ftp/pub
#測試下載
cd /tmp
lftp $ipaddr <<end
cd pub
get hosts
exit
end

if [ -f /tmp/hosts ];then
	echo "匿名使用者下載成功"
	rm -f /tmp/hosts
else
	echo "匿名使用者下載失敗"
fi
#測試上傳、建立目錄、刪除目錄等
cd /tmp
lftp $ipaddr << end
cd pub
mkdir test1
mkdir test2
put /etc/group
rmdir test2
exit
end

if [ -d /var/ftp/pub/test1 ];then
    echo "建立目錄成功"
	if [ ! -d /var/ftp/pub/test2 ];then
    	echo "檔案刪除成功"
        fi
else
	if [ -f /var/ftp/pub/group ];then
	echo "檔案上傳成功"
        else
        echo "上傳、建立目錄刪除目錄部ok"
        fi 
fi   
[ -f /var/ftp/pub/group ] && echo "上傳檔案成功"