awk学习--例子篇

来源:互联网 发布:综武侠一朝成名天下知 编辑:程序博客网 时间:2024/05/16 17:01

转自:chinaunix


awk例子参考:
精确匹配
awk ‘$3==“48” {print$0}’ grade.txt
awk ‘$0 !~ /Brown/’ grade.txt
awk ‘$4!=“Brown-2” {print $0}’ grade.txt
awk ‘{if($6 < $7) print $0}’ grade.txt
设置大小写 可使用[ ]符号
awk ‘/[Gg]reen/’ grade.txt
任意字符
awk ‘$1 ~ /^…a/’ grade.txt
或关系匹配
awk ‘$0 ~/(Yellow|Brown)/’ grade.txt
行首
awk ‘/^M/’ grade.txt
AND
awk ‘{if ($1"P.Bunny" && $4“Yellow”) print $0}’ grade.txt
Or
awk ‘{if ($4==“Yellow” || $4~/Brown/) print $0}’
因为awk默认是输出$0的,所以一般不需要加上:
print $0
要快速查看记录个数,应使用N R
awk ‘END{print NR}’ grade.txt
N F变量显示每一条读记录中有多少个域,并在E N D部分打印输入文件名。
awk ‘{print NF,NR,$0} END{print FILENAME}’ grade.txt
awk ‘{if (NR>0 && $4~/Brown/)print $0}’ grade.txt
echo $PWD | awk -F/ ’ {print $NF}’
N F的一个强大功能是将变量$ P W D的返回值传入a w k并显示其目录。这里需要指定域分隔符/
echo “/usr/local/etc/rc.sybase” | awk -F/ ‘{print $NF}’
echo $PWD | awk ‘{print $NF}’
awk ‘{name=$1;belts=$4;if(belts ~/Yellow/) print name" is belt "belts}’ grade.txt
awk ‘{if ($6<$7) print $0}’ grade.txt
awk ‘BEGIN{BASELINE="27"} {if ($6<BASELINE) print $0}’ grade.txt
awk ‘{if($1==“M.Tans”) {$6=$6-1};print $1,$6,$7}’ grade.txt
awk ‘{if($1==“M.Tans”) {$6=$6-1;print $1,$6,$7}}’ grade.txt
awk ‘BEGIN{print “Name\tDifference”}{if($6<$7) {$8=$7-$6;print $1,$8}}’ grade.txt
awk ‘BEGIN{print “Name\tDifference”}{if($6<$7) {diff=$7-$6;print $1,diff}}’ grade.txt
awk ‘(tot+=$6); END{print “Club student total points :” tot}’ grade.txt
awk ‘{(tot+=$6)}; END{print “Club student total points :” tot}’ grade.txt
文件长度相加
ls -l | awk ‘/^[^d]/ {print $9"\t"$5} {tot+=$5} END {print “total KB:” tot}’
awk ‘gsub(/4842/,4899){print $0}’ grade.txt
awk ‘gsub(/4842/,4899)’ grade.txt
awk ‘BEGIN {print index(“Bunny”,“ny”)}’ grade.txt
awk ‘$1==“J.Troll” {print length($1)" "$1}’ grade.txt
awk ‘BEGIN{print length(“A FEW GOOD MEN”)}’
awk ‘BEGIN{print match(“ANCD”,/d/)}
awk ’BEGIN{print match(“ANCD”,/D/)}’
awk ‘$1==“J.Lulu” {print match($1,“u”)}’ grade.txt
awk ‘BEGIN {print split(“123-456-789”,pats_array,“-”)}’
awk ‘BEGIN {print split(“123#456#789”,myarray,“#”)}’
awk ‘$1==“J.Troll” sub(/26/,“29”,$0)’ grade.txt
awk ‘$1==“L.Tansl” {print substr($1,1,3)}’ grade.txt
awk ‘$1==“L.Tansl” {print substr($1,1,99)}’ grade.txt
awk ‘{print substr($1,3)}’ grade.txt
awk ‘BEGIN{STR=“A FEW GOOD MEN”}END{print substr(STR,7)}’ grade.txt
从s h e l l中向a w k传入字符串
echo “Stand-by” | awk ‘{print length($0)}’
STR=“mydoc.txt”
echo $STR|awk ‘{print substr($STR,1,5)}’
STR=“mydoc.txt”
echo $STR|awk ‘{print substr($STR,7)}’
echo “65” | awk ‘{printf “%c\n”,$0}’
awk ‘BEGIN{printf “%c\n”,65}’
awk ‘BEGIN{printf “%f\n”,999}’
格式化输出
awk ‘{printf “%-15s %s\n”,$1,$3}’ grade.txt
awk ‘BEGIN{print “Name\t\tS.Number”}{printf “%-15s %s\n”,$1,$3}’ grade.txt
向一行a w k命令传值
awk ‘{if ($5<AGE) print $0}’ AGE=10 grade.txt
快速查看文件系统空间容量
df -k|awk ‘{if($4<TRIGGER) print $6"\t"$4}’ TRIGGER=560000
df -k|awk ‘($4~/^[0-9]/) {if($4<TRIGGER) print $6"\t"$4}’ TRIGGER=5600000
who |awk ‘{print $1" is logged on"}’
pwd | awk ‘{if ($1==derr) print $1}’ derr=$HOME
grade.txt:
M.Tans 5/99 48311 Green 8 40 44
J.Lulu 06/99 48317 green 9 24 26
P.Bunny 02/99 48 Yellow 12 35 28
J.Troll 07/99 4842 aBrown-3 12 26 26
L.Tansl 05/99 4712 Brown-2 12 30 28
awk脚本文件
a.awk
chmod 700 a.awk
#!/bin/awk -f
#all commnet lines must start with a hash ‘#’
#name:students_tots.awk
#to call:student_tot.awk grade.txt
#prints total and average of club student points
#print a header first
BEGIN{
print “Student Date Member No. Grade Age Points Max”
print “Name Joined Gained Point Available”
print “==============”
}
#let’s add the scores of points gained
(tot+=$6)
#finished proessing now let’s print the total and average point
END{
print “Club student total points :” tot
print “Average Club Student Points:” tot/NR}
a w k脚本
在a w k中使用F S变量
#!/bin/awk -f
#to call:passwd.awk /etc/passwd
#print out the first and seventh fields
BEGIN{
FS=“:”}
{print $1,“\t”,$7}
向a w k脚本传值
awk script_file var=value input_file
#!/bin/awk -f
#check on how many fields in a file
#name:fieldcheck.awk
#to call:fieldcheck MAX=n FS= filename
#
NF!=MAX{
print(“line” NR " does not have " MAX “fields”)}
./fieldcheck.awk MAX=7 FS=“:” passwd
awk
数组
awk ‘BEGIN {print split(“123#456#789”,myarray,“#”)}’
实际上m y a r r a y数组为
Myarray1=“123”
Myarray2=“456”
Myarray3=“789”
#!/bin/awk -f
#name:arraytest.awk
#prints out an array
BEGIN{
record=“123#456#789”;
split(record,myarray,“#”)}
END{for (i in myarray) {print myarray[i]}}
./arraytest.awk /dev/null
数组和记录
[sam@Linux_chenwy sam]$ cat grade_student.txt
Yellow#Junior
Orange#Senior
Yellor#Junior
Purple#Junior
Brown-2#Junior
White#Senior
Orange#Senior
Red#Junior
Red#Junior
Brown-2#Senior
Yellow#Senior
Red#Junior
Blue#Senior
Green#Senior
Purple#Junior
White#Junior
[sam@Linux_chenwy sam]$ cat belts.awk
#!/bin/awk -f
#name:belts.awk
#to call:belts.awk grade2.txt
#loops through the grade2.txt file and counts how many
#belts we have in (yellow,orange,red)
#also count how many adults and juniors we have
#
#start of BEGIN
#set FS and load the arrays with our values
#B E G I N部分设置F S为符号#,即域分隔符
BEGIN{FS=“#”
#Load the belt colours we are interested in only
#因为要查找Ye l l o w、O r a n g e和R e d三个级别。
#然后在脚本中手工建立数组下标对学生做同样的操作。
#注意,脚本到此只有下标或元素,并没有给数组名本身加任何注释。
belt[“Yellow”]
belt[“Orange”]
belt[“Red”]
#end of BEGIN
#load the student type
student[“Junior”]
student[“Senior”]
}
##初始化完成后, B E G I N部分结束。记住B E G I N部分并没有文件处理操作。
#loop thru array that holds the belt colours against field-1
#if we have a match,keep a running total
#现在可以处理文件了。
#首先给数组命名为c o l o r,使用循环语句测试域1级别列是否
#等于数组元素之一(Ye l l o w、O r a n g e或R e d),
#如果匹配,依照匹配元素将运行总数保存进数组。
{for (colour in belt)
{if($1==colour)
belt[colour]++}}
#loop thru array that holds the student type against
#field-2 if we have a match,keep a runing total
#同样处理数组‘ S e n i o r _ o r _ j u n i o r’,
#浏览域2时匹配操作满足,运行总数存入j u n i o r或s e n i o r的匹配数组元素。
{for (senior_or_junior in student)
{if ($2==senior_or_junior)
student[senior_or_junior]++}}
#finished processing so print out the matches..for each array
#E N D部分打印浏览结果,对每一个数组使用循环语句并打印它。
END{for (colour in belt )print "The club has “,belt[colour],colour,”Belts"
#注意在打印语句末尾有一个\符号,用来通知a w k(或相关脚本)命令持续到下一行,
#当输入一个很长的命令,并且想分行输入时可使用这种方法。
for (senior_or_junior in student) print "The club has ",\
student[senior_or_junior],senior_or_junior,"student"}
./belts.awk grade_student.txt


原创粉丝点击