awk -F: '{print "Number of dields: "NF}' passwd        字段分隔符设为冒号,所以每条记录的字段数变成7;

awk  '{print "Number of dields: "NF}' passwd          字段分隔符设为空格,所以每条记录的字段数不一致;

awk -F:  '{print $1}' passwd                  以冒号为分隔符打印出文本第一字段;

awk '{print $1}' passwd                   已空格符为分隔符打印出文本第一字段;

awk '/root/{print $0}' passwd                如果记录模式中包含root,则打印该记录($0);

awk '$1 ~ /nologin$/{print "The price is $" $1 "."}' passwd   如果第一个字段以nologin结尾,则打印双引号之间的字符串、第一个字段($1)、和只含一个句点的字符;

awk '$1 ~ /[a-z][a-z]/{print $1}' passwd            第一个字段以两个小写字母结尾的,则打印该字段;

awk '$1 ~ /^root/{print $1 "is a super man."}' passwd     若第一个字段以root开头的,则打印该字段并在后面接上字符串“is a super man.”,注意若要打印空格则应该把包含在字符串中;

awk '$1 !~ /e/{print $1,$2} ' passwd            如果第一个字段中不含模式e则打印该字段;

awk '$1 ~ /\.[7-9]+/' passwd                 如果某条记录的第一个字段包含一个句点,而且句点后是一个或多个7-9之间的数字,就打印该记录;

awk '/^[rv]/{print $1}' passwd               打印以r或v开头的模式中的第一字段;

awk '/e/' passwd                     打印记录中有e的就打印整条记录;

awk '/root/{print $1,"+++++",$2}' passwd           如果记录中包含root则打印第一字段 +++++ 第二字段;

awk '{print "Number of fileds: "  NF}' passwd        打印记录中的字段值;

awk '{print $0}' passwd                 $0是打印当前的记录;

awk '{print $2 $1}'  passwd                第二字段与第一字段之间不用空格进行隔开;

awk '{print $2,$1}' passwd                 第二字段与第一字段之间用空格进行隔开;

awk '/^(root|sync)/' passwd                打印所有以root和sync开头的行;

awk '/^root/' passwd                   打印所有以root开头的行;

awk ‘root/’  passwd                    打印所有包含root的行;

gawk '/[[:lower:]]+n[[:space:]]+[[:digit:]]/' passwd        gawk搜索一个或多个小写字母后面跟一个n后面再跟一个或多个空格然后是一个数字的模式,awk并不能识别POSIX增加的括号字符类;

awk '$1 !~ /nologin$/' passwd              显示所有第一个字段非nologin结尾的;

awk '$1 ~ /[Ss]hd/' passwd                显示所有在第一字段匹配到Shd或者shd的行;

nawk '/^[A-Z][a-z]+ /' passwd                 匹配开头以一个大写字母跟一个或多个小写字母,再跟一个空格的行(注意awk和gawk在此命令中是不区分大小写的,nawk是区分大小写的);

awk '/^root/' passwd                   匹配以root开头的行;

awk '$1 < 4000' passwd

awk '$0 ~ /root/{print $0}' passwd

awk '/root/{print "Hello there, "$1}' passwd

awk -F: '/root/{print $0}' passwd

awk -F: '/root/{print $1,$2,$3,$4}' passwd

awk -F'[ :\t]' '{print $1,$2,$3}' passwd

awk -F: '/root/{print $1,$2}' passwd

awk '{print NR,$1,$2}' passwd

awk '{print $0,NF}' passwd

awk '{print NR,$0}' passwd

nawk '{printf "The name is: %-15s ID is %8d\n",$1,$2}' passwd    要打印的字符串放在两个引号之间。第一个格式说明符是%-15s,他对应的参数是$1,紧挨着控制穿的右半边引号后面的那个逗号。百分号引出格式说明;短划线表示左对齐,15s表示占15格的字符串。这条命令用来打印一个左对齐、占15格字符串,后面跟着字符串的ID和一个整数;格式%8d表示在字符串的这个位置打印$2的十进制整数值。这个整数占8格,向右对齐。

echo "UNIX" | awk '{printf "|%15s|\n",$1}'       右对齐

echo "UNIX" | awk '{printf "|%-15s|\n",$1}'       左对齐

awk 'BEGIN{OFMT="%.2f";print 1.23456789,12E-2}'        设置变量OFMT在打印浮点数时,只打印小数部分的前两位。百分号表示接下来要定义格式;

awk '/root/{print "\t\tHave a nice day, "$1,$2 "!"}' passwd       在awk中!不用进行转义,nawk中需要进行转义;

nawk '/root/{print "\t\tHave a nice day, "$1,$2 "\!"}' passwd

date | awk '{print "Month: " $2 "\nYear: ",$6}'

awk比较表达式:

1 awk '$3 == 11111' datafile
2 awk '$3 > 11111{print $1}' datafile
3 awk '$2 ~ /bbb/' datafile
4 awk '$2 !~ /bbb/' datafile
5 awk '{max=($1>$2)?$1:$2;print max}' datafile
6 awk '{max=($3>$4)?$3:$4;print max}' datafile
7 awk '{max=($3>$4)?$3:$4;print max}' datafile
8 awk '$3*$4 > 11111111' datafile
9 awk '$3*$4 > 111111111' datafile
10 awk '$3*$4 > 1111111111' datafile
11 awk '$3*$4 > 111111111' datafile
12 awk '$3>11111 && $4 <=44444' datafile
13 awk '$3 == 22222 || $4>22222' datafile
14 awk '!($3<44444 && $4>11111)' datafile
15 awk '!($3<44444 && $4>11111)' datafile
16 awk '/aaa/,/eee/' datafile
17 awk '{print NF,$0}' passwd
18 awk -F: '{print NF,$0}' passwd
19 awk '$3 == 11111' datafile
20 awk '$2 == "bbb"' datafile
21 awk '$2 == "bbb"{print $1,$2}' datafile
22 awk '$3 != 11111' datafile
23 awk '$3>11111{print $4,$5}' datafile
24 awk '$3>.9{print $4,$5}' datafile
25 awk '$3<44444{print $4,$5}' datafile
26 awk '$3>=33333{print $5}' datafile
27 awk '$3<=33333{print $5}' datafile
28 awk '$8>10 && $8<17' datafile
29 awk '$3 == NR ||$1 ~ /south/{print$1,$2}' datafile
30 awk '!($8 == 13){print $8}' datafile
31 awk '/aaa/{print $3+11111}' datafile
32 awk '/aaa/{print $3+11111.11}' datafile
33 awk '/aaa/{print $3+11111}' datafile
34 awk '/aaa/{print $3+11111.22}' datafile
35 awk '/aaa/{print $3-11111.22}' datafile
36 awk '/aaa/{print $3-1111.22}' datafile
37 awk '/aaa/{print $3/2}' datafile
38 awk '/aaa/{print $3*2}' datafile
39 awk '/aaa/{print $3/3}' datafile
40 awk '/aaa/{print $3%3}' datafile
41 awk '/^western/,/^eastern/' datafile
42 awk '{print ($7>4 ? "high" $7:"low" $7)}' datafile
43 awk '$3 == "Chris"{$3 = "Christian";print}' datafile
44 awk '$3 == "11111"{$3 = "11111111111111111111111";print}' datafile
45 awk '/Break/{$8 += 12;print $8}' datafile
46 awk '/aaa/{$3 += 99;print $3}' datafile
47 awk '{$7 %= 3;print $7}' datafile
48 awk '{$3 %= 3;print $3}' datafile

变量及管道

1 awk '$1 ~ /aaa/{wage= $3 * $4;print wage}' datafile
2 awk '$1 ~ /aaa/{wage= $3 +=1;print wage}' datafile
3 awk '$1 ~ /aaa/{wage= $3 -=1;print wage}' datafile
4 awk '{$6 = 1000 * $3 / $4; print}' datafile
5 awk '$1 == "CA" {$1 = "California";print}' datafile
6 awk -F: '$1 == "Mary Adams"{print NR,$1,$2,$NF}' datafile
7 awk -F: '{IGNORECASE=1}; $1 == "mary adams"{print NR,$1,$2,$NF}' datafile
8 awk 'BEGIN{FS=":";OFS="\t";ORS="\n\n"}{print $1,$2,$3}' datafile
9 awk 'BEGIN{print "MAKE YEAR"}'
10 awk 'BEGIN{FS=":";OFS="\t";ORS="\n\n"}{print $1,$2,$3}'
11 awk 'END{print "The number of records is " NR}' datafile
12 awk '/Mary/{count++}END{print "Mary was found " count " times."}' datafile
13 awk '$4 >= 70 {print $1,$2 > "passing_file"}' datafile
14 awk 'BEGIN{"date" | getline d;print d}' datafile
15 awk 'BEGIN{"date" | getline d;split(d,mon);print mon[2]}' datafile
16 awk 'BEGIN{while ("ls" | getline) print}'
17 awk 'BEGIN{printf "What is your name?";name < "/dev/tty"} "See ya, " name "."}' datafile
18 awk 'BEGIN{while (getline < "/test/datafile" > 0 )c++; print c}' datafile
19 history | awk '$1 > 1007 {print $0 }' | awk '$1=NR {print $0}'>> 2017-10-17

20 awk '{x=$3--;print "x= "x,"$3= "$3}' passwd
21 awk -F: '{x=$3--;print "x= "x,"$3= "$3}' passwd
22 awk '/^north/{print "The record number is " NR"}' passwd
23 awk '{print NR,$0}' passwd

24 awk -F: '{print $1,$2 | "sort -r +1 -2 +0 -1"}' passwd

nawk '/^Tom/{name[NR]=$1};END{for (i in name){print name[i]}}' db
nawk '/^Tom/{name[NR]=$1};END{for (i in name){print name[i]}}' db
awk '/^Tom/{name[NR]=$1};END{for (i in name){print name[i]}}' db
cat awk.sc
#awk.sc script
/tom/{count["tom"]++}
/mary/{count["mary"]++}
END{print "There are "count["tom"]" Toms in the file and "count["mary"]" Marys in the file."}
awk -f awk.sc datafile3
awk '{dup[$2]++;if (dup[$2] > 1 ){name[$2]++}}END{for (i in name)print i name[i]}' datafile4
nawk BEGIN'{split("3/15/2004",date,"/"); print "The mouth is " date[1] "and the year is "date[3]}' datafile4
awk BEGIN'{split("3/15/2004",date,"/"); print "The mouth is " date[1] "and the year is "date[3]}' datafile4
nawk '{line[x++]=$2}END{for (x in line) delete line[x]}' datafile4

  • awk多维数组

[root@www 2017-10-24]# cat nf.sh
{nf=NF
for (x=1;x<=NF;x++){
  matrix[ NR,x ]= $x
  }
}
END{for (x=1;x<=NR;x++){
  for (y=1;y<=nf;y++)
  printf "%d ",matrix[x,y]
printf"\n"
  }
}

[root@www 2017-10-24]# awk -f nf.sh datafile
1 2 3 4 5
2 3 4 5 6
6 7 8 9 10

  • awk处理命令行参数

ARGV:数组的下标从0开始

ARGC:是一个包含命令行参数个数的内置变量

[root@www 2017-10-24]# awk -f argvs datafile
argv[0] is awk
argv[1] is datafile
argv[2] is
The number of arguments,ARGC=2

[root@www 2017-10-24]# cat argvs
#Scriptname: argvs
BEGIN{
for ( i=0;i<=ARGC;i++ ){
printf ( "argv[%d] is %s\n",i,ARGV[i] )
}
printf ("The number of arguments,ARGC=%d\n",ARGC)
}

未完待续……(200)

05-26 17:56