shell高級-----初識sed和gawk


sed編輯器

sed說明

sed是Linux下一款功能強大的非交互流式文本編輯器,可以對文本文件進行增、刪、改、查等操作,支持按行、按字段、按正則匹配文本內容,靈活方便,特別適合於大文件的編輯。

替換選項

使用s命令可以實現替換的作用。S命令會用斜線間指定的第二個文本字符串來替換第一個文本字符串模式;

echo "this is a test" | sed 's/test/try/'  

如果要同時替換多個,sed -e 中間用分號;隔開即可

1、替換標記

默認情況下,只會替換一行中的第一處。要想替換一行中不同地方出現的文件必須使用替換標記。

s/pattern/replacement/flags

有四種可用的替換標志:

  • 數字:表明新文件將替換第幾處模式匹配的地方,比如2,替換每行中第二次出現的文本
  • g :表明新文件將會替換所有匹配的文本
  • p : 打印出匹配的內容 ,通常與sed的-n一起使用
  • w file :將替換的結果寫入到文件中
[root@node3 ljy]# more ceshi.sh                  
this is one,one,one
[root@node3 ljy]# sed -i 's/one/two/2 ' ceshi.sh                                         
[root@node3 ljy]# more ceshi.sh                  
this is one,two,one
[root@node3 ljy]# sed -i 's/one/two/g ' ceshi.sh  
[root@node3 ljy]# more ceshi.sh                  
this is two,two,two

-n選項是禁止sed編輯器輸出,-p會輸出修改過的行。這二者配合使用的效果就是只輸出被替換命令修改過的行。

2、替換字符

替換文件中的特殊字符會比較麻煩,比如你要替換路徑符號/

sed允許其他字符來替換命令中的字符串分隔符。

echo "/ljy/ceshi" | sed 's!/ljy/ceshi!/ljy/test!'

 注意末尾的替換符。

使用地址

如果想要命令作用於特定的行或某些行,則必須使用行尋址。

1、以數字方式行尋址

在命令中指定的地址可以是單個行號,或者用起始行號,逗號以及結尾行號指定一定區間。

[root@node1 ljy]# sed '2s/cat/dog/' cat
this is a cat
this is a dog
this is a cat
this is a cat
this is a cat
this is a cat
this is a cat
this is a cat
[root@node1 ljy]# sed '2,3s/cat/dog/' cat
this is a cat
this is a dog
this is a dog
this is a cat
this is a cat
this is a cat
this is a cat
this is a cat
[root@node1 ljy]# sed '2,$s/cat/dog/' cat  
this is a cat
this is a dog
this is a dog
this is a dog
this is a dog
this is a dog
this is a dog
this is a dog

2、使用文本模式過濾器

sed編輯器允許指定文本模式來過濾出命令要做用的行。

[root@node1 ljy]# more cat 
there is a cat
this is a cat
this is a cat
this is a cat
this is a cat
this is a cat
this is a cat
this is a cat
[root@node1 ljy]# sed '/there/s/cat/dog/' cat 
there is a dog
this is a cat
this is a cat
this is a cat
this is a cat
this is a cat
this is a cat
this is a cat

3、組合命令

[root@node1 ljy]# sed '2,${
> s/cat/dog/
> s/this/there/
> }' cat
there is a cat
there is a dog
there is a dog
there is a dog
there is a dog
there is a dog
there is a dog
there is a dog

可以用花括號將多條命令組合在一起使用。

刪除行

刪除命令d

[root@node1 ljy]# sed '4d' num
this is 1
this is 2
this is 3
this is 5
[root@node1 ljy]# sed '3,$d' num 
this is 1
this is 2
[root@node1 ljy]# sed '/this/d' num

插入和附加文本

插入命令i會在指定行前增加一個新行

附加命令a會在指定行后增加一個新行。

[root@node1 ljy]# sed '2i\this is a test' num 
this is 1
this is a test
this is 2
this is 3
[root@node1 ljy]# sed '2a\this is a test' num  
this is 1
this is 2
this is a test
this is 3
[root@node1 ljy]# sed '$a\this is a test' num 
this is 1
this is 2
this is 3
this is a test

修改行

修改命令允許修改數據流中整行文本的內容。

[root@node1 ljy]# sed '3c\this is a test' num 
this is 1
this is 2
this is a test
[root@node1 ljy]# sed '/this is 3/c\this is a test' num    #文本模式也可以
this is 1
this is 2
this is a test

轉換命令

轉換命令(y)是唯一可以處理單個字符的sed編輯器命令。

[root@node1 ljy]# sed 'y/2/9/' num    
this is 1
this is 9
this is 3

回顧打印

p命令用來打印文本行

=命令用來打印行號

l命令用來列出行

[root@node1 ljy]# sed -n '/is 2/p' num 
this is 2
[root@node1 ljy]# sed '=' num            
1
this is 1
2
this is 2
3
this is 3
[root@node1 ljy]# sed 'l' num  
this is 1$
this is 1
this is 2$
this is 2
this is 3$
this is 3

使用sed處理文件

1、寫入文件

w命令用來向文件寫入行

[root@node1 ljy]# sed '1,2w ceshi' num 
this is 1
this is 2
this is 3
[root@node1 ljy]# more ceshi 
this is 1
this is 2

把num的前兩行寫入到了ceshi文件中

2、從文件讀取數據

讀取命令r允許你將一個獨立文件中的數據插入到另一個數據流中。

[root@node1 ljy]# more ceshi1
this is a
this is b
[root@node1 ljy]# more ceshi2
hello
hi
[root@node1 ljy]# sed '2r ceshi1' ceshi2
hello
hi
this is a
this is b

gawk程序

gawk提供了一種編程語言而不只是編程命令。

1、命令格式

gawk options program file*

options的可用選項有:

 -F fs 指定行中分隔數據字段的字段分隔符。個人不建議使用這個選項,在BEGIN塊中設置FS更好。這個選項只是提供了一個簡潔的設置方式。
 -f file:指定讀取程序的文件名
 -v var=value 定義gawk程序中的一個變量及其默認值。個人不建議使用這個選項,在BEGIN塊中設置更好。
 -mf N 指定要處理的數據文件中的最大字段數
 -mr N 指定數據文件中的最大數據行數
 -W keyword 指定gawk的兼容模式或警告等級

2、從命令行讀取腳本

[root@node1 ~]# awk '{print "hello"}'
asd
hello
adf
hello
asd
hello
qqq
hello
[root@n

要終止這個gawk程序,你必須表明數據流已經結束,

ctrl+D組合鍵可以在bash中產生一個EOF字符。

3、使用數據字段變量

默認情況下,gawk會將如下變量分配給它在文本中發現的數據字段:

$0  代表整個文本行
$1  代表文本行的第一個數據段
$n  代表文本行的第n個數據段
$NF  代表文本行的最后一個數據段

gwak中默認的字段分隔符書任意的空白字符。

[root@node1 ~]# df -h | gawk '{print $5}'
已用%
5%
0%
0%
1%
0%
14%
0%
[root@node1 ~]# df -h | gawk '{print $NF}'
掛載點
/
/dev
/dev/shm
/run
/sys/fs/cgroup
/boot
/run/user/0
[root@node1 ~]# df -h | gawk '{print $0}' 
文件系統                 容量  已用  可用 已用% 掛載點
/dev/mapper/centos-root   42G  2.1G   40G    5% /
devtmpfs                 908M     0  908M    0% /dev
tmpfs                    920M     0  920M    0% /dev/shm
tmpfs                    920M  8.8M  911M    1% /run
tmpfs                    920M     0  920M    0% /sys/fs/cgroup
/dev/sda1               1014M  142M  873M   14% /boot
tmpfs                    184M     0  184M    0% /run/user/0

4、在程序腳本中使用多個命令

[root@node1 ~]# echo 'this is sam' | gawk '{$4="lisi";print $0}'  
this is sam lisi

5、從文件中讀取程序

gawk編輯器允許將程序存儲到文件中,然后在命令行中引用。

[root@node1 ljy]# more script.gawk 
{print $1 "'s home directory is " $6}
[root@node1 ljy]# gawk -F: -f script.gawk /etc/passwd
root's home directory is /root
bin's home directory is /bin
daemon's home directory is /sbin
adm's home directory is /var/adm
lp's home directory is /var/spool/lpd
sync's home directory is /sbin
shutdown's home directory is /sbin
halt's home directory is /sbin
mail's home directory is /var/spool/mail
operator's home directory is /root
games's home directory is /usr/games
ftp's home directory is /var/ftp
nobody's home directory is /
systemd-network's home directory is /
dbus's home directory is /
polkitd's home directory is /
sshd's home directory is /var/empty/sshd
postfix's home directory is /var/spool/postfix
chrony's home directory is /var/lib/chrony
mysql's home directory is /var/lib/mysql
dockerroot's home directory is /var/lib/docker
ljy's home directory is /home/ljy

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM