awk中級篇
這里順便介紹一下vi的一個替換命令,現(xiàn)在我們要把example1.txt文本里的空格都替換為“:”冒號這里在vi里使用的命令就是:
%s/ /:/g
這個命令對于使用vi的人來說是用得最多的。我們現(xiàn)在做了一個新的文件example2.txt。
user1:password1:username1:unit1:10
user2:password2:username2:unit2:20
user3:password3:username3:unit3:30
現(xiàn)在我們來做一個awk腳本,之前都是在命令行操作,實際上所有的操作在命令行上是可以都實現(xiàn)的,已我們最經(jīng)常使用的批量添加用戶來開始!
script1.awk
#!/bin/awk -f # 當文件有可執(zhí)行權限的時候你可以直接執(zhí)行
# ./script1.awk example2.txt
# 如果沒有以上這行可能會出現(xiàn)錯誤,或者
# awk -f script1.awk example2.txt 參數(shù)f指腳本文件
BEGIN { # “BEGIN{”是awk腳本開始的地方
FS=":"; # FS 是在awk里指分割符的意思
}
{ # 接下來的“{” 是內(nèi)容部分
print "add {"; # 以下都是使用了一個awk函數(shù)print
print "uid=" $1;
print "userPassword=" $2;
print "domain=eyou.com" ;
print "bookmark=1";
print "voicemail=1";
print "securemail=1"
print "storage=" $5;
print "}";
print ".";
} # “}” 內(nèi)容部分結(jié)束
END { # “END{” 結(jié)束部分
print "exit";
}
執(zhí)行結(jié)果
[root@mail awk]# awk -f script1.awk example2.txt
add {
uid=user1
userPassword=password1
domain=eyou.com
bookmark=1
voicemail=1
securemail=1
storage=10
}
.
.
.
.
.
.
exit
文本操作就是更方便一些。
下面給兩個返回效果一樣的例子
[root@mail awk]# awk -F: '{print $1"@"$2}' example2.txt
[root@mail awk]# awk -F: '{printf "%s@%s",$1,$2}' example2.txt
user1@password1
這里的區(qū)別是使用print 和printf的區(qū)別,printf格式更自由一些,我們可以更加自由的指定要輸出的數(shù)據(jù),print會自動在行尾給出空格,而printf是需要給定" "的,如果感興趣你可以把“”去掉看一下結(jié)果。%s代表字符串%d 代表數(shù)字,基本上%s都可以處理了因為在文本里一切都可以看成是字符串,不像C語言等其他語言還要區(qū)分數(shù)字、字符、字符串等。
awk還有一些很好的函數(shù)細細研究一下還是很好用的。
這次碰到了一個問題客戶有一個用戶列表,大概有2w用戶,他有一個有趣的工作要做,就是把每個賬戶目錄放到特定的目錄下,例如13910011234這個目錄要放到139/10/這個目錄下,從這里可以看出規(guī)律是手機號碼的前三位是二級目錄名,手機的第3、4為是三級目錄名,我們有的就只有一個用戶列表,規(guī)律找到了我們現(xiàn)在開始想辦法處理吧。
example3.txt
13910011234
15920312343
13922342134
15922334422
......
第一步是要找到一個方法來吧,就是要把每一個手機號分開,最初可能你就會想到這個也沒有任何間隔,我們怎么用awk分開他們呢?說實話最初我也考慮了20多分鐘,后來想起原來學習python的時候有split函數(shù)可以分就想找找awk里是不是有類似的函數(shù),man awk 發(fā)現(xiàn)substr 這個函數(shù)子串,
[root@mail awk]# awk '{print substr($1,1,3)}' example3.txt
[root@mail awk]# awk '{printf "%s/%s",substr($1,1,3),substr($1,4,2)}' example3.txt
[root@mail awk]# awk '{printf "mv %s %s/%s",$1,substr($1,1,3),substr($1,4,2)}' example3.txt
以上的兩步的返回自己做一下,最后我們就得到了我們想要的結(jié)果。
mv 13910011234 139/10
mv 15920312343 159/20
mv 13922342134 139/22
mv 15922334422 159/22
把這部分輸出拷貝到一個shell腳本里,在數(shù)據(jù)當前目錄下執(zhí)行就可以了!
substr(s, i [, n]) Returns the at most n-character substring of s
starting at i. If n is omitted, the rest of s
is used.
substr函數(shù)解釋,s代表我們要處理的字符串,i 是我們從這個字符串的第幾個位置上開始,n 是我們從開始的位置取多少個字符。多看看man英文也會有所提高的。
awk有很多有趣的函數(shù)如果感興趣可以自己去查查看,
man awk
String Functions 字符串函數(shù),舉幾個覺得常用的函數(shù)
length([s]) Returns the length of the string s, or the
length of $0 if s is not supplied.
length 你可以得到字符串的長度,這個是比較常用的一個函數(shù)
split(s, a [, r]) Splits the string s into the array a on the
regular expression r, and returns the number of
fields. If r is omitted, FS is used instead.
The array a is cleared first. Splitting
behaves identically to field splitting,
described above.
tolower(str) Returns a copy of the string str, with all the
upper-case characters in str translated to
their corresponding lower-case counterparts.
Non-alphabetic characters are left unchanged.
toupper(str) Returns a copy of the string str, with all the
lower-case characters in str translated to
their corresponding upper-case counterparts.
Non-alphabetic characters are left unchanged.
Time Functions 時間函數(shù),我們最最常用到的是時間戳轉(zhuǎn)換函數(shù)
strftime([format [, timestamp]])
Formats timestamp according to the specification in format.
The timestamp should be of the same form as returned by sys-
time(). If timestamp is missing, the current time of day is
used. If format is missing, a default format equivalent to
the output of date(1) is used. See the specification for the
strftime() function in ANSI C for the format conversions that
are guaranteed to be available. A public-domain version of
strftime(3) and a man page for it come with gawk; if that
version was used to build gawk, then all of the conversions
described in that man page are available to gawk.
這里舉例說明時間戳函數(shù)是如何使用的
[root@ent root]# date +%s | awk '{print strftime("%F %T",$0)}'
2008-02-19 15:59:19
我們先使用date命令做一個時間戳,然后再把他轉(zhuǎn)換為時間
還有一些我們現(xiàn)在可能不經(jīng)常用到的函數(shù),詳細內(nèi)容man awk 自己可以看一下。
Bit Manipulations Functions 二進制函數(shù)
Internationalization Functions 國際標準化函數(shù)
USER-DEFINED FUNCTIONS 用戶也可以自己定義自己的函數(shù),感興趣自己可以再深入研究一下。
For example:
function f(p, q, a, b) # a and b are local
{
...
}
/abc/ { ... ; f(1, 2) ; ... }
DYNAMICALLY LOADING NEW FUNCTIONS 動態(tài)加載新函數(shù),這個可能就更高級一些了!
新聞熱點
疑難解答