sed行編輯器
sed:(stream editor )用來操作純 ASCII 碼的文本,一次處理一行内容,處理時,把目前的行存儲在臨時緩沖區,稱之為“模式空間”,處理完後把緩沖區的内容輸送到螢幕,接着處理下一行,這樣不斷重複,直到檔案末尾。
sed指令格式
sed [參數] ‘指令’ file
sed [參數] '指令' file
p ##顯示
d ##删除
a ##添加
c ##替換
i ##插入
- p:
[[email protected] mnt]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Wed May 7 01:22:57 2014
#
# 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
#
UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 / xfs defaults 1 1
/dev/sr0 /rhel7.0 iso9660 defaults 0 0
[[email protected] mnt]#sed -n '/\:/p' /etc/fstab ##顯示含有“:” 的行
# Created by anaconda on Wed May 7 01:22:57 2014
[[email protected] mnt]#sed -n '/^#/p' /etc/fstab ##顯示以“#”開頭的行
#
# /etc/fstab
# Created by anaconda on Wed May 7 01:22:57 2014
#
# 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 ##顯示不以“#”開頭的行
UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 / xfs defaults 1 1
/dev/sr0 /rhel7.0 iso9660 defaults 0 0
[[email protected] mnt]#sed -n '2,6p' /etc/fstab ##顯示2~6 行
#
# /etc/fstab
# Created by anaconda on Wed May 7 01:22:57 2014
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
[[email protected] mnt]#sed -n '2,6!p' /etc/fstab ##顯示除2~6 行之外的其他行
#
# /etc/fstab
# Created by anaconda on Wed May 7 01:22:57 2014
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
[[email protected] mnt]# sed -n '2,6!p' /etc/fstab
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 / xfs defaults 1 1
/dev/sr0 /rhel7.0 iso9660 defaults 0 0
- d:
[[email protected] mnt]# sed '/^UUID/d' /etc/fstab ##删除以UUID開頭的行
#
# /etc/fstab
# Created by anaconda on Wed May 7 01:22:57 2014
#
# 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/sr0 /rhel7.0 iso9660 defaults 0 0
[[email protected] mnt]# sed '/^#/d' /etc/fstab ##删除以“#'"開頭的行
UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 / xfs defaults 1 1
/dev/sr0 /rhel7.0 iso9660 defaults 0 0
[[email protected] mnt]# sed '/^$/d' /etc/fstab ##删除空行
#
# /etc/fstab
# Created by anaconda on Wed May 7 01:22:57 2014
#
# 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
#
UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 / xfs defaults 1 1
/dev/sr0 /rhel7.0 iso9660 defaults 0 0
[[email protected] mnt]# sed '1,4d' /etc/fstab ##删除1~4行
#
# 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
#
UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 / xfs defaults 1 1
/dev/sr0 /rhel7.0 iso9660 defaults 0 0
- a:
sed '/hello/aworld' westos
sed 's/hello/hello world/g' westos
sed 's/hello/hello\nworld/g' westos

- c:替換
sed '/hello/chello world' westos
- i:
[[email protected] mnt]# sed '/hello/iworld\nwestos' westos
world
westos
hello
-i:改變原檔案内容
sed -i 's/westos/redhat/' passwd
sed -i 's/westos/redhat/g' passwd ##全局替換
awk報告生成器
awk處理機制:根據模式一次從檔案中抽取一行文本,對這行文本進行切片(預設使用空白字元作為分隔符)
1.Awk 是被設計用于文本處理,并通常被用作資料提取和報告工具的解釋性程式設計語言
2.目前在Linux中常用的是 awk 編譯版本有 mawk 、gawk
3.以 RedHat 為代表使用的是 gawk,以Ubuntu為代表使用的是 mawk
[[email protected] mnt]# cat test
this | is | a | file
$1 $2 $3 $4
[[email protected] mnt]#awk '{print $0}' test ##$0表示輸出一整行
[[email protected] mnt]#awk '{print $1}' test
[[email protected] mnt]#awk '{print $4}' test
[[email protected] mnt]#awk '{print $1,$2}' test ##顯示兩個字段
[ro[email protected] mnt]#awk -F ":" '{print $1,$3}' /etc/passwd ##指定分隔符
- awk常用變量
[[email protected] mnt]#awk '{print FILENAME,NR}' /etc/passwd ##輸出檔案名,和目前操作的行号
[[email protected] mnt]#awk -F: '{print NR,NF}' /etc/passwd ##輸出每次處理的行号,以及目前以":"為分隔符的字段個數
總結:awk '{print "第NR行","有NF列"}' /etc/passwd
BEGIN{}:讀入第一行文本之前執行的語句,一般用來初始化操作
{}:逐行處理
END{}:處理完最後以行文本後執行,一般用來處理輸出結果
awk 'BEGIN { a=34;print a+10 }'
awk -F: 'BEGIN{print "REDHAT"} {print NR;print } END {print "WESTOS"}' westos ##檔案開頭加REDHAT,末尾加WESTOS,列印行号和内容
awk -F: '/bash$/{print}' /etc/passwd ##輸出以bash結尾的
awk -F: 'NR==3 {print}' /etc/passwd
awk -F: 'NR % 2 == 0 {print}' /etc/passwd ##偶數行
awk -F: 'NR >=3 && NR <=5 {print }' /etc/passwd
awk 'BEGIN{i=0}{i+=NR}END{print i}' linux.txt ##統計文本總行數
awk的進階應用
- if單分支語句
awk -F: 'BEGIN{i=0}{if($7~/bash$/){i++}}END{print i}' /etc/passwd ##統計登入shell為bash的使用者
awk -F: 'BEGIN{i=0}{if($3<500){i++}}END{print i}' /etc/passwd ##統計/etc/passwd下uid小于500的使用者個數
- if雙分支
awk -F: 'BEGIN{i=0;j=0}{if($3<=500){i++}else{j++}}END{print i,j}' /etc/passwd ##統計uid小于等于500和大于500的使用者個數
- for循環
awk 'BEGIN{for(i=1;i<=5;i++){print i}}' ## 生成随機數
- while循環
[[email protected] mnt]# awk 'i=0{}BEGIN{while(i<3){++i;print i}}' test
1
2
3
[[email protected] mnt]# awk 'BEGIN{do{++i;print i}while(i<3)}' test
1
2
3