两个有用的shell工具总结
shell工具之一:sed
sed基礎(chǔ)
sed編輯器被稱作流編輯器,與常見的交互式文本編輯器剛好相反。文本編輯器可以通過鍵盤來交互式地插入、刪除、替換文本中的數(shù)據(jù);而流編輯器是基于一組預(yù)先的規(guī)則來編輯數(shù)據(jù)流。
sed命令的格式如下:
sed options script file
|
選項(xiàng) |
說明 |
|
-e script |
將script中指定的命令添加到運(yùn)行的命令中 |
|
-f file |
將file中指定的命令添加到運(yùn)行的命令中 |
|
-n |
不為每個(gè)命令生成輸出,等待print命令來輸出 |
說明:
script用于指定作用在數(shù)據(jù)量上的單個(gè)命令。
如果需要使用多個(gè)命令,有兩種選擇:可以在命令行中使用-e選項(xiàng)指定,不同命令之間用分號隔開;或者使用-f選項(xiàng)在文件中指定。
默認(rèn)情況下,sed編輯器將指定的命令應(yīng)用到STDIN輸入流上,而不作用于數(shù)據(jù)源本身,就是說sed不會修改文本文件中的原數(shù)據(jù)。
1 替換命令substitute
s/pattern/replacement/flags
flags取值如下:
數(shù)字:表示replacement將替換每行中第幾次出現(xiàn)的pattern
g:表示replacement將替換所有出現(xiàn)的pattern
p:打印用replacement替換過的行(經(jīng)常與-n選項(xiàng)搭配使用,-n禁止sed輸出,而p會輸出修改過的行,二者搭配則僅輸出被replacement替換過的行)
w file:將替換的結(jié)果寫到file文件中(只有被替換過的行才會保存到輸出文件file中)
[root@benxintuzi shell]# cat data1 benxin benxin benxin benxin benxin benxin benxin benxin benxin benxin benxin benxin benxin benxin benxin [root@benxintuzi shell]# sed 's/benxin/tuzi/3' data1 benxin benxin tuzi benxin benxin benxin benxin tuzi benxin benxin benxin benxin tuzi benxin benxin [root@benxintuzi shell]# sed 's/benxin/tuzi/g' data1 tuzi tuzi tuzi tuzi tuzi tuzi tuzi tuzi tuzi tuzi tuzi tuzi tuzi tuzi tuzi [root@benxintuzi shell]# cat data2 benxin benxin benxin benxin benxin benxin benxin benxin benxin benxin benxin benxin benxin benxin benxin hello hello hello hello hello hello [root@benxintuzi shell]# sed -n 's/benxin/tuzi/p' data2 tuzi benxin benxin benxin benxin tuzi benxin benxin benxin benxin tuzi benxin benxin benxin benxin [root@benxintuzi shell]# sed 's/benxin/tuzi/w out' data2 tuzi benxin benxin benxin benxin tuzi benxin benxin benxin benxin tuzi benxin benxin benxin benxin hello hello hello hello hello hello [root@benxintuzi shell]# cat out tuzi benxin benxin benxin benxin tuzi benxin benxin benxin benxin tuzi benxin benxin benxin benxin
2 指定作用行
默認(rèn)情況下,sed會作用于所有行,如果只想作用于特定行,必須使用行尋址,格式如下:
[address]command或者
address
{
command1
command2
command3
...
}
address可以使用單個(gè)行號,或者是起始行號、逗號、結(jié)尾行號指定。
[root@benxintuzi shell]# sed '2s/benxin/tuzi/' data1
benxin benxin benxin benxin benxin
tuzi benxin benxin benxin benxin
benxin benxin benxin benxin benxin
[root@benxintuzi shell]# sed '2,$s/benxin/tuzi/' data1
benxin benxin benxin benxin benxin
tuzi benxin benxin benxin benxin
tuzi benxin benxin benxin benxin
[root@benxintuzi shell]# sed '2,${
s/benxin/tuzi/3
s/hello/world/2
}' data2
benxin benxin benxin benxin benxin
benxin benxin tuzi benxin benxin
benxin benxin tuzi benxin benxin
hello world hello hello hello hello
delete命令會刪除指定模式的所有行,如果沒有加入行尋址,則會刪除流中的所有文本(并不修改原文件)。
3 插入和追加
insert命令i在指定行前增加一個(gè)新行;
append命令a在指定行后增加一個(gè)新行。
格式如下:
sed '[address]command
ew line'
[root@benxintuzi shell]# cat data1 benxin benxin benxin benxin benxin benxin benxin benxin benxin benxin benxin benxin benxin benxin benxin [root@benxintuzi shell]# sed '3i uzi tuzi tuzi' data1 benxin benxin benxin benxin benxin benxin benxin benxin benxin benxin tuzi tuzi tuzi benxin benxin benxin benxin benxin [root@benxintuzi shell]# sed '3a uzi tuzi tuzi' data1 benxin benxin benxin benxin benxin benxin benxin benxin benxin benxin benxin benxin benxin benxin benxin tuzi tuzi tuzi
4 轉(zhuǎn)換命令transform
轉(zhuǎn)換命令y可以處理單個(gè)字符,格式如下:
[address]y/inchars/outchars/
用outchars的第一個(gè)字符替換inchars的第一個(gè)字符,outchars的第二個(gè)字符替換inchars的第二個(gè)字符,...,如果inchars和outchars的長度不同,則sed編輯器產(chǎn)生一個(gè)錯誤。
[root@benxintuzi shell]# sed 'y/bn/ti/' data1 teixii teixii teixii teixii teixii teixii teixii teixii teixii teixii teixii teixii teixii teixii teixii
5 打印命令
p: 打印文本行
=: 打印行號
l: 打印文本行和不可打印的ASCII字符
[root@benxintuzi shell]# cat data2
benxin benxin benxin benxin benxin
benxin benxin benxin benxin benxin
benxin benxin benxin benxin benxin
hello hello hello hello hello hello
[root@benxintuzi shell]# sed -n '2,4p' data2
benxin benxin benxin benxin benxin
benxin benxin benxin benxin benxin
hello hello hello hello hello hello
[root@benxintuzi shell]# sed '=' data2
1
benxin benxin benxin benxin benxin
2
benxin benxin benxin benxin benxin
3
benxin benxin benxin benxin benxin
4
hello hello hello hello hello hello
[root@benxintuzi shell]# sed -n '/hello/{
> =
> p
> }' data2
4
hello hello hello hello hello hello
[root@benxintuzi shell]# sed -n 'l' data2
benxin benxin benxin benxin benxin$
benxin benxin benxin benxin benxin$
benxin benxin benxin benxin benxin$
hello hello hello hello hello hello$
6 文件命令
[address]w filename
[address]r filename
[root@benxintuzi shell]# cat data3 This is the line 1 This is the line 2 This is the line 3 This is the line 4 [root@benxintuzi shell]# sed -n '3,4w outfile' data3 [root@benxintuzi shell]# cat outfile This is the line 3 This is the line 4 [root@benxintuzi shell]# sed '1r outfile' data3 This is the line 1 This is the line 3 This is the line 4 This is the line 2 This is the line 3 This is the line 4
sed進(jìn)階
1 多行命令
N: 將數(shù)據(jù)流中的下一行加進(jìn)來創(chuàng)建一個(gè)多行組
D: 刪除多行組中的第一行
P: 打印多行組中的第一行
[root@benxintuzi shell]# cat data3
This is the line 1
This is the line 2
This is the line 3
This is the line 4
[root@benxintuzi shell]# sed '/line 2/{N; s/line/number/g}' data3
This is the line 1
This is the number 2
This is the number 3
This is the line 4
2 跳轉(zhuǎn)命令
[address]b [label]
address決定了哪些行會觸發(fā)跳轉(zhuǎn)命令;label定義了要跳轉(zhuǎn)的位置,如果沒有l(wèi)abel參數(shù),那么將跳轉(zhuǎn)到腳本的末尾。定義label:開始,最多可以有7個(gè)字符。
[root@benxintuzi shell]# cat data3
This is the line 1
This is the line 2
This is the line 3
This is the line 4
[root@benxintuzi shell]# sed '{2,3b; s/line/number/g}' data3
This is the number 1
This is the line 2
This is the line 3
This is the number 4
[root@benxintuzi shell]# sed '{
2,3b
s/line/number/g
:label
s/the/a/g
}' data3
This is a number 1
This is the line 2
This is the line 3
This is a number 4
3 測試命令
[address]t [label]
測試命令的跳轉(zhuǎn)不是基于地址,而是基于替換命令是否成功。如下程序每次去除一個(gè)逗號,直到一個(gè)逗號都沒有時(shí)結(jié)束循環(huán)跳轉(zhuǎn)。
[root@benxintuzi shell]# echo "This,is,a,test,to,remove,commas." | sed -n '{
:start
s/,/ /p
t start
}'
This is,a,test,to,remove,commas.
This is a,test,to,remove,commas.
This is a test,to,remove,commas.
This is a test to,remove,commas.
This is a test to remove,commas.
This is a test to remove commas.
4 模式替換
在行The cat sleeps in a hat中,如果想將cat和hat都加上引號,如何做呢?sed中可以使用&表示找到的匹配模式:
[root@benxintuzi shell]# echo "The cat sleeps in a hat" | sed 's/.at/".at"/g' The ".at" sleeps in a ".at" [root@benxintuzi shell]# echo "The cat sleeps in a hat" | sed 's/.at/"&"/g' The "cat" sleeps in a "hat"
5 sed實(shí)例工具
# 加倍行間距G:$!G表示最后一行不加倍 [root@benxintuzi shell]# cat data3 This is the line 1 This is the line 2 This is the line 3 This is the line 4 [root@benxintuzi shell]# sed '$!G' data3 This is the line 1 This is the line 2 This is the line 3 This is the line 4 [root@benxintuzi shell]#
# 行編號工具: [root@benxintuzi shell]# cat data3 This is the line 1 This is the line 2 This is the line 3 This is the line 4 [root@benxintuzi shell]# sed '=' data3 | sed 'N; s/ / /' 1 This is the line 1 2 This is the line 2 3 This is the line 3 4 This is the line 4
# 刪除多余的空白行: # 關(guān)鍵在于創(chuàng)建一個(gè)包含非空白行和一個(gè)緊挨空白行的地址區(qū)間,匹配該區(qū)間則不執(zhí)行刪除。 # /./,/^$/!d,/./表示至少包含一個(gè)字符的行,/^$/表示一個(gè)空行: [root@benxintuzi shell]# cat data4 This is the line 1 This is the line 2 This is the line 3 This is the line 4 [root@benxintuzi shell]# sed '/./,/^$/!d' data4 This is the line 1 This is the line 2 This is the line 3 This is the line 4
# 刪除開頭的空行: [root@benxintuzi shell]# cat data5 This is the line 1 This is the line 2 This is the line 3 This is the line 4 [root@benxintuzi shell]# sed '/./,$!d' data5 This is the line 1 This is the line 2 This is the line 3 This is the line 4
# 刪除結(jié)尾的空行:
[root@benxintuzi shell]# cat data6
This is the line 1
This is the line 2
This is the line 3
This is the line 4
[root@benxintuzi shell]# sed '{
> :start
> /^
*$/{$d; N; b start}
> }' data6
This is the line 1
This is the line 2
This is the line 3
This is the line 4
[root@benxintuzi shell]#
shell工具之二:gawk
gawk基礎(chǔ)
gawk程序是unix中原始awk程序的GNU版本。gawk提供了一種編程環(huán)境,允許修改和重新組織文件中的數(shù)據(jù)。格式如下:
gawk options program file
|
選項(xiàng) |
說明 |
|
-F fs |
指定行中分隔數(shù)據(jù)字段的分隔符 |
|
-f file |
指定gawk程序的文件名 |
|
-v var=value |
定義gawk程序中的一個(gè)變量及其默認(rèn)值 |
|
-mf N |
指定要處理的數(shù)據(jù)文件中的最大字段數(shù) |
|
-mr N |
指定要處理的數(shù)據(jù)文件中的最大行數(shù) |
|
-W keyword |
指定gawk的兼容模式或警告等級 |
gawk程序使用的腳本命令必須放在用單引號括起來的花括號中:
gawk '{print "hello benxintui"}'
gawk在處理文本文件時(shí),自動為行中的每個(gè)數(shù)據(jù)字段分配一個(gè)變量:
"$0"表示整行;
"$1"表示行中第1個(gè)字段;
"$2"表示行中第2個(gè)字段;
...
"$n"表示行中第n個(gè)字段。
gawk還可以指定程序何時(shí)運(yùn)行。
在處理數(shù)據(jù)前運(yùn)行腳本,可以使用BEGIN關(guān)鍵字:
[root@benxintuzi shell]# cat data3
This is the line 1
This is the line 2
This is the line 3
This is the line 4
[root@benxintuzi shell]# gawk 'BEGIN{print "The data3 File Contents:"} {print $0}' data3
The data3 File Contents:
This is the line 1
This is the line 2
This is the line 3
This is the line 4
在處理數(shù)據(jù)后運(yùn)行腳本,可以使用END關(guān)鍵字:
[root@benxintuzi shell]# gawk 'BEGIN{print "The data3 File Contents:"} {print $0} END{print "End of File"}' data3
The data3 File Contents:
This is the line 1
This is the line 2
This is the line 3
This is the line 4
End of File
gawk進(jìn)階
1 使用變量
內(nèi)置變量:
1 字段和行分隔變量
|
變量 |
說明 |
|
FS |
輸入字段分隔符(默認(rèn)為空格) |
|
RS |
輸入行分隔符(默認(rèn)為換行符) |
|
OFS |
輸出字段分隔符(默認(rèn)為空格) |
|
ORS |
輸出行分隔符(默認(rèn)為換行符) |
[root@benxintuzi shell]# cat data7
This,is,the,line,1
This,is,the,line,2
This,is,the,line,3
This,is,the,line,4
[root@benxintuzi shell]# gawk 'BEGIN{FS=","; OFS="<-->"; RS="
"; ORS="|
"} {print $1,$2,$3,$4,$5}' data7
This<-->is<-->the<-->line<-->1|
This<-->is<-->the<-->line<-->2|
This<-->is<-->the<-->line<-->3|
This<-->is<-->the<-->line<-->4|
[root@benxintuzi shell]# gawk 'BEGIN{FS=","; OFS="<-->"; RS="
"; ORS="|
"} {print $0}' data7
This,is,the,line,1|
This,is,the,line,2|
This,is,the,line,3|
This,is,the,line,4|
2 數(shù)據(jù)變量
|
變量 |
說明 |
|
ARGC |
當(dāng)前命令行參數(shù)個(gè)數(shù) |
|
ARGV |
當(dāng)前命令行參數(shù)數(shù)組 |
|
ARGIND |
當(dāng)前文件在ARGV中的位置 |
|
CONVFMT |
數(shù)字的轉(zhuǎn)換格式,默認(rèn)為%.6g |
|
OFMT |
數(shù)字的輸出格式,默認(rèn)為%.6g |
|
ENVIRON |
當(dāng)前shell環(huán)境變量及其值組成的關(guān)聯(lián)數(shù)組 |
|
ERRNO |
錯誤號 |
|
FILENAME |
數(shù)據(jù)文件名 |
|
FNR |
數(shù)據(jù)文件中的當(dāng)前行數(shù) |
|
NF |
數(shù)據(jù)文件中每行的字段數(shù) |
|
NR |
已處理的數(shù)據(jù)行數(shù)(如果同時(shí)處理多個(gè)文件,則該值會不斷累加) |
|
IGNORECASE |
當(dāng)其值非0時(shí),忽略gawk命令中字符串的大小寫 |
|
RLENGTH |
由match匹配的子字符串的長度 |
|
RSTART |
由match匹配的子字符串的起始位置 |
[root@benxintuzi shell]# gawk 'BEGIN{print ARGC, ARGV[0], ARGV[1]}' data7
2 gawk data7
[root@benxintuzi shell]# gawk 'BEGIN{print ENVIRON["HOME"]; print ENVIRON["PATH"]}'
/root
/usr/lib/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/java/jdk1.7.0_75/bin:/usr/java/jdk1.7.0_75/jre/bin:/bigdata/hadoop-2.6.0/bin:/bigdata/hadoop-2.6.0/sbin:/usr/maven/apache-maven-3.3.3/bin:/home/benxintuzi/bin
[root@benxintuzi shell]# gawk '
BEGIN{FS=","}
{print "FILENAME="FILENAME, "ARGIND="ARGIND, "NF="NF", FNR="FNR, "NR="NR}' data7 data7
FILENAME=data7 ARGIND=1 NF=5, FNR=1 NR=1
FILENAME=data7 ARGIND=1 NF=5, FNR=2 NR=2
FILENAME=data7 ARGIND=1 NF=5, FNR=3 NR=3
FILENAME=data7 ARGIND=1 NF=5, FNR=4 NR=4
FILENAME=data7 ARGIND=2 NF=5, FNR=1 NR=5
FILENAME=data7 ARGIND=2 NF=5, FNR=2 NR=6
FILENAME=data7 ARGIND=2 NF=5, FNR=3 NR=7
FILENAME=data7 ARGIND=2 NF=5, FNR=4 NR=8
自定義變量:
gawk變量名區(qū)分大小寫。
[root@benxintuzi shell]# gawk '
BEGIN{
testing="This is a test"
print testing
testing=45
print testing
}'
This is a test
45
# 在命令行中給gawk變量賦值:
[root@benxintuzi shell]# cat script1
BEGIN{FS=","}
{print $n}
[root@benxintuzi shell]# gawk -f script1 n=2 data7
is
is
is
is
[root@benxintuzi shell]# gawk -f script1 n=5 data7
1
2
3
4
2 操作數(shù)組
# 數(shù)組的定義
[root@benxintuzi shell]# gawk '
> BEGIN{
> capital["China"]="beijing"
> print capital["China"]
> var[1]=20
> var[2]=10
> total=var[1] + var[2]
> print total
> }'
beijing
30
# 數(shù)組的遍歷
[root@benxintuzi shell]# gawk '
BEGIN{
var["a"]=1
var["b"]=2
var["c"]=3
for (dex in var)
{
print "Index:" dex " --- " "Value:" var[dex]
}
}'
Index:a --- Value:1
Index:b --- Value:2
Index:c --- Value:3
# 數(shù)組元素的刪除
[root@benxintuzi shell]# gawk '
BEGIN{
var["a"]=1
var["b"]=2
var["c"]=3
for (dex in var)
{
print "Index:" dex " --- " "Value:" var[dex]
}
delete var["b"]
print "-------------------"
for (dex in var)
{
print "Index:" dex " --- " "Value:" var[dex]
}
}'
Index:a --- Value:1
Index:b --- Value:2
Index:c --- Value:3
-------------------
Index:a --- Value:1
Index:c --- Value:3
3 使用模式
使用正則表達(dá)式來匹配時(shí),表達(dá)式必須放到作用腳本的花括號左邊:
[root@benxintuzi shell]# cat data7
This,is,the,line,1
This,is,the,line,2
This,is,the,line,3
This,is,the,line,4
[root@benxintuzi shell]# gawk 'BEGIN{FS=","} /,2/{print $0}' data7
This,is,the,line,2
使用匹配操作符˜來匹配數(shù)據(jù)行中的特定字段,如找出以1開頭的第二個(gè)字段的行,如下所示:
[root@benxintuzi shell]# cat out1
This is a test program 2684
Loop 1
Loop 2
Loop 3
Loop 4
Loop 5
Loop 6
Loop 7
Loop 8
Loop 9
Loop 10
This is the end of program
[root@benxintuzi shell]# gawk '$2 ~ /^1/{print $0}' out1
Loop 1
Loop 10
還可以使用比較表達(dá)式(== / <= / < / >= / >)來匹配,如顯示所有屬于root用戶組的系統(tǒng)用戶(組ID==0):
[root@benxintuzi shell]# cat /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin gopher:x:13:30:gopher:/var/gopher:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin rtkit:x:499:497:RealtimeKit:/proc:/sbin/nologin avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin pulse:x:498:496:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin haldaemon:x:68:68:HAL daemon:/:/sbin/nologin ntp:x:38:38::/etc/ntp:/sbin/nologin apache:x:48:48:Apache:/var/www:/sbin/nologin saslauth:x:497:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin abrt:x:173:173::/etc/abrt:/sbin/nologin gdm:x:42:42::/var/lib/gdm:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin benxintuzi:x:500:500:CentOS:/home/benxintuzi:/bin/bash
[root@benxintuzi shell]# gawk -F: '$4 == 0{print $1}' /etc/passwd root sync shutdown halt operator
4 結(jié)構(gòu)化命令
|
if格式: if (condition) { statements } else { statements } |
if (condition) statements; else statements |
|
while格式: while (condition) { statements } |
do { statements } while (condition) |
|
for格式: for (assignments; condition; iteration) |
5 內(nèi)置函數(shù)
|
數(shù)學(xué)函數(shù) |
說明 (采用弧度為單位) |
|
atan2(x, y) |
x/y的反正切 |
|
cos(x) |
x的余弦值 |
|
exp(x) |
x的指數(shù)函數(shù) |
|
int(x) |
x的整數(shù)部分 |
|
rand() |
0~1的隨機(jī)浮點(diǎn)數(shù) |
|
sin(x) |
x的正弦值 |
|
sqrt(x) |
x的平方根 |
|
srand(x) |
種子函數(shù) |
|
位操作函數(shù) |
說明 |
|
and(v1, v2) |
v1與v2按位與 |
|
or(v1, v2) |
v1與v2按位或 |
|
compl(val) |
對val求補(bǔ) |
|
lshift(val, count) |
將val左移count位 |
|
rshift(val, count) |
將val右移count位 |
|
xor(v1, v2) |
v1與v2按位異或 |
|
字符串函數(shù) |
說明 |
|
asort(s [, d]) |
對數(shù)組s按數(shù)據(jù)元素排序。排序后數(shù)組的索引值變?yōu)楸硎卷樞虻臄?shù)字。如果指定了d,則將排序后的數(shù)組存儲到d中 |
|
asorti(s [, d]) |
對數(shù)組s按索引值進(jìn)行排序。排序后數(shù)組的元素為表示順序的索引值。如果指定了d,則將排序后的數(shù)組存儲到d中 |
|
gensub(r, s, h [, t]) |
查找變量$0或者目標(biāo)字符串t來匹配正則表達(dá)式r。如果h以g/G開頭,則用s替換掉匹配的文本;如果h為數(shù)字,表示要替換掉第幾處匹配的文本 |
|
gsub(r, s [, t]) |
查找變量$0或者目標(biāo)字符串t來匹配正則表達(dá)式r。如果找到了,就全部替換為字符串s |
|
index(s, t) |
返回t在s中的索引值。如果沒找到,則返回0 |
|
length([s]) |
返回字符串s的長度。如果沒有指定s,則返回$0的長度 |
|
match(s, r, [, a]) |
返回字符串s中正則表達(dá)式r位置的索引值。如果指定了a,則將s中匹配r的部分存儲到a中 |
|
split(s, a [, r]) |
將s用FS字符或者r分隔開存儲到a中,返回被分割的行數(shù) |
|
sprintf(format, variables) |
類似于printf |
|
sub(r, s [, t]) |
在變量$0或者目標(biāo)字符串t中查找正則表達(dá)式r的匹配,如果找到了,就用s替換掉第一處匹配 |
|
substr(s, i [, n]) |
返回s中從索引值i開始的n個(gè)字符構(gòu)成的字符串。如果未指定n,則返回i開始的所有部分 |
|
tolower(s) |
將s中的所有字符轉(zhuǎn)換為小寫 |
|
toupper(s) |
將s中的所有字符轉(zhuǎn)換為大寫 |
# 按數(shù)組元素排序
[root@benxintuzi shell]# gawk '
BEGIN{
var["d"]=4
var["b"]=3
var["a"]=2
var["c"]=1
asort(var, result)
for (i in result)
print "index:" i " <---> " "value:" result[i]
}'
index:4 <---> value:4
index:1 <---> value:1
index:2 <---> value:2
index:3 <---> value:3
# 按數(shù)組索引排序
[root@benxintuzi shell]# gawk '
BEGIN{
var["d"]=4
var["b"]=3
var["a"]=2
var["c"]=1
asorti(var, result)
for (i in result)
print "index:" i " <---> " "value:" result[i]
}'
index:4 <---> value:d
index:1 <---> value:a
index:2 <---> value:b
index:3 <---> value:c
[root@benxintuzi shell]# cat data7
This,is,the,line,1
This,is,the,line,2
This,is,the,line,3
This,is,the,line,4
[root@benxintuzi shell]# gawk '
BEGIN{FS=","}
{
number=split($0, var)
print($number, var[1], var[2], var[3], var[4], var[5])
}' data7
1 This is the line 1
2 This is the line 2
3 This is the line 3
4 This is the line 4
|
|
|
時(shí)間函數(shù) |
說明 |
|
mktime(datespec) |
將一個(gè)YYYY MM DD HH MM SS [DST]格式的日期轉(zhuǎn)換成時(shí)間戳 |
|
strftime(format [, timestamp]) |
將當(dāng)前時(shí)間的時(shí)間戳轉(zhuǎn)換為shell函數(shù)date()的格式 |
|
systime() |
返回當(dāng)前時(shí)間的時(shí)間戳 |
[root@benxintuzi shell]# gawk '
> BEGIN{
> date=systime()
> day=strftime("%A %B %d %Y", date)
> print day
> }'
Sunday August 16 2015
|
|
6 自定義函數(shù)
格式:
function name ([variables])
{
statements
}
函數(shù)的定義必須出現(xiàn)在所有代碼塊之前,包括BEGIN塊。如果將函數(shù)放在庫文件中定義,則在gawk中使用時(shí),利用-f選項(xiàng)指定庫文件。但是如果需要同時(shí)通過-f指定腳本文件名,則利用多次-f即可:
# 在命令行中定義函數(shù)
[root@benxintuzi shell]# gawk '
function myfunc()
{
print($1, $2, $5)
}
BEGIN{FS=","}
{ myfunc() }' data7
This is 1
This is 2
This is 3
This is 4
# 在庫文件中定義函數(shù)
[root@benxintuzi shell]# cat funclib
function myfunc()
{
print($1, $2, $5)
}
[root@benxintuzi shell]# cat script2
BEGIN{FS=","; RS="
"}
{
myfunc()
}
[root@benxintuzi shell]# gawk -f funclib -f script2 data7
This is 1
This is 2
This is 3
This is 4
總結(jié)
以上是生活随笔為你收集整理的两个有用的shell工具总结的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 、恐怖组织Blitz是哪一年对巨人城发起
- 下一篇: 瞬间让电脑更酷了瞬间让电脑更酷了表情包