$ vi employee-one-line.txt
101,John Doe:102,Jason Smith:103,Raj Reddy:104,Anand
Ram:105,Jane Miller
# 使用RS=":" 对输入文件中的行进行分割成新的行
$ awk -F, 'BEGIN { RS=":" } \
{ print $2 }' employee-one-line.txt
John Doe
Jason Smith
Raj Reddy
Anand Ram
Jane Miller
$ awk'BEGIN { FS="\n"; RS="-\n"; OFS=":" } \
{print $2, $3}' employee-change-fs-ofs.txt
John Doe:CEO
Jason Smith:IT Manager
Raj Reddy:Sysadmin
Anand Ram:Developer
Jane Miller:Sales Manager
$ awk'BEGIN { FS=","; ORS="\n---\n" } \
{print $2, $3}' employee.txt
John Doe CEO
---
Jason Smith IT Manager
---
Raj Reddy Sysadmin
---
Anand Ram Developer
---
Jane Miller Sales Manager
---
$ awk'BEGIN { FS=","; OFS="\n";ORS="\n---\n" } \
{print $1,$2,$3}' employee.txt
NR – Number of Records (行号)
FILENAME – 当前文件名
FNR – 文件 Number of Records (行号) 对多个文件时使用
$ awk'BEGIN {FS=","} \
{print "Emp Id of record number",NR,"is",$1;} \
END {print "Total number of records:",NR}' employee.txt
Emp Id of record number 1 is 101
Emp Id of record number 2 is 102
Emp Id of record number 3 is 103
Emp Id of record number 4 is 104
Emp Id of record number 5 is 105
Total number of records: 5
$ awk'BEGIN {FS=","} \
{print FILENAME ": record number",FNR,"is",$1;} \
END {print "Total number of records:",NR}'\
employee.txt employee-multiple-fs.txt
employee.txt: record number 1 is 101
employee.txt: record number 2 is 102
employee.txt: record number 3 is 103
employee.txt: record number 4 is 104
employee.txt: record number 5 is 105
employee-multiple-fs.txt: record number 1 is 101
employee-multiple-fs.txt: record number 2 is 102
employee-multiple-fs.txt: record number 3 is 103
employee-multiple-fs.txt: record number 4 is 104
employee-multiple-fs.txt: record number 5 is 105
Total number of records: 10
Chr10 awk 变量和运算符
$ cat total-company-salary.awk
BEGIN {FS=",";total=0;}{
print $2"'s salary is: "$4;total=total+$4}
END {
print "---\nTotal company salary = $"total;}
$ awk -f total-company-salary.awk employee-sal.txt
John Doe's salary is: 10000
Jason Smith's salary is: 5000
Raj Reddy's salary is: 4500
Anand Ram's salary is: 4500
Jane Miller's salary is: 3000
---
Total company salary = $27000
# 比较++ 在前在后的区别
$ awk -F, '{print ++$4}' employee-sal.txt
10001
$ awk -F, '{print --$4}' employee-sal.txt
9999
$ awk -F ',' '{print $4++}' employee-sal.txt
10000
$ awk -F ',' '{$4++; print $4}' employee-sal.txt
10001
# 某列求和
$ awk -F ',' 'BEGIN {total=0}{total+=$5} END
{print "Total Quantity: " total}' items.txt
比较运算符
> Is greater than
>= Is greater than or equal to
< Is less than
<= Is less than or equal to
== Is equal to
!= Is not equal to
&& Both the conditional expressions are true|| Either one of the conditional expressions is true
$ awk -F ","'$3 != "Video" {print $2}' items.txt
$ awk -F ","'$4 < 900 && $5 <= 5 {print $2}' items.txt
$ awk -F ","'$4 < 900 || $5 <= 5 {print $2}' items.txt
# 取最大的maxuid
$ awk -F ':''$3 > maxuid { maxuid=$3; maxline=$0 }; \
END { print maxuid, maxline }' /etc/passwd
$ awk -F ':''$3>=100 && $NF ~ /\/bin\/sh/' /etc/passwd
Chr11 awk 条件从句和循环
# 使用方法if(conditional-expression){
action1;
action2;}
$ awk -F ","'{ if ($5 <= 5) \
print "Only",$5,"qty of",$2, "is available"; }'\
items.txt
$ awk -F ","\'{ if ( ($4 >= 500 && $4 <= 1000) && ($5 <= 5)) \
print "Only",$5,"qty of",$2,"is available";}' items.txt
# if else 语句
$ cat if-else.awk
BEGIN {FS=",";}{if($5<=5)
print "Buy More: Order", $2, "immediately!"else
print "Sell More: Give discount on", $2,
"immediately!"}
$ awk -f if-else.awk items.txt
Sell More: Give discount on HD Camcorder immediately!
Buy More: Order Refrigerator immediately!
Sell More: Give discount on MP3 Player immediately!
Sell More: Give discount on Tennis Racket immediately!## while 语句
$ awk'BEGIN \
{ while (count++<50) string=string "x"; print string }'
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
$ cat while.awk
{i=2;total=0;while(i <= NF){
total = total + $i;
i++;}
print "Item", $1, ":", total, "quantities sold";}## do while 语句
$ cat dowhile.awk
{i=2;total=0;do{
total = total + $i;
i++;}while(i <= NF)
print "Item", $1, ":", total, "quantities sold";}## for 循环语句
$ cat for.awk
{total=0;for(i=2; i <= NF; i++)
total = total + $i;
print "Item", $1, ":", total, "quantities sold";}# break
$ awk'BEGIN{
x=1;
while(1)
{
print "Iteration";
if ( x==10 )
break;
x++;
}}'## continue 跳过该次循环,不破坏当前循环
$ cat continue.awk
{i=1;total=0;while(i++ <= NF){if(i ==1)continue;
total = total + $i;}
print "Item", $1, ":", total, "quantities sold";}# exit 退出循环
$ awk'BEGIN{
x=1;
while(x<=10)
{
if(x==5){
exit;}
print "Value of x",x;x++;
}
}'