sed是Linux下一款功能強大的非交互流式文本編輯器,能夠對文本文件進行增、刪、改、查等操做,支持按行、按字段、按正則匹配文本內容,靈活方便,特別適合於大文件的編輯。node
使用s命令能夠實現替換的做用。S命令會用斜線間指定的第二個文本字符串來替換第一個文本字符串模式;mysql
echo "this is a test" | sed 's/test/try/'
若是要同事替換多個,中間yoga分號;隔開便可sql
一、替換標記docker
默認狀況下,只會替換一行中的第一處。要想替換一行中不一樣地方出現的文件必須使用替換標記。編程
s/pattern/replacement/flagscentos
有四種可用的替換標誌:bash
[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會輸出修改過的行。這兩者配合使用的效果就是隻輸出被替換命令修改過的行。app
替換文件中的特殊字符會比較麻煩,好比你要替換路徑符號/ssh
sed容許其餘字符來替換命令中的字符串分隔符。編程語言
echo "/ljy/ceshi" | sed 's!/ljy/ceshi!/ljy/test!'
注意末尾的替換符。
若是想要命令做用於特定的行或某些行,則必須使用行尋址。
一、以數字方式行尋址
在命令中指定的地址能夠是單個行號,或者用起始行號,逗號以及結尾行號指定必定區間。
[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
二、使用文本模式過濾器
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
三、組合命令
[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
一、寫入文件
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文件中
二、從文件讀取數據
讀取命令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 options program file*
options的可用選項有:
-F fs 指定行中分隔數據字段的字段分隔符。我的不建議使用這個選項,在BEGIN塊中設置FS更好。這個選項只是提供了一個簡潔的設置方式。 -f file:指定讀取程序的文件名 -v var=value 定義gawk程序中的一個變量及其默認值。我的不建議使用這個選項,在BEGIN塊中設置更好。 -mf N 指定要處理的數據文件中的最大字段數 -mr N 指定數據文件中的最大數據行數 -W keyword 指定gawk的兼容模式或警告等級
[root@node1 ~]# awk '{print "hello"}' asd hello adf hello asd hello qqq hello [root@n
要終止這個gawk程序,你必須代表數據流已經結束,
ctrl+D組合鍵能夠在bash中產生一個EOF字符。
默認狀況下,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
[root@node1 ~]# echo 'this is sam' | gawk '{$4="lisi";print $0}' this is sam lisi
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