Sed and Awk 101 Hacks --2 AWK学习

Sed and Awk 101 Hacks <2> AWK学习

Chr8、awk语法和脚本命令

  1. awk 命令语法
awk -Fs '/pattern/ {action}' input-file 
(or)
awk -Fs '{action}' intput-file

$ awk -F: '/mail/ {print $1}' /etc/passwd 
mail 

awk -Fs -f myscript.awk input-file
  1. awk 程序结构(BEGIN,body,END,block)
$ awk 'BEGIN { FS=":";print "---header---" } \
/mail/ {print $1} \
END { print "---footer---"}' /etc/passwd
# 注意 awk -F:  在分割输入文件每行时等价于 awk 'BEGIN {FS=":"}'
$ awk 'BEGIN { FS=":";print "---header---" } \
/mail/ {print $1} \
END { print "---footer---"}' /etc/passwd /etc/group 
  1. Print Command
$ awk -F ',' 'BEGIN \
{ print "-------------\nName\tTitle\n-------------"} \
{ print $2,"\t",$3;} \
END { print "-------------"; }' employee.txt 
  1. 模式匹配
$ awk -F ',' '/Manager/ {print $2, $3}' employee.txt 
$ awk -F ',' '/^102/ {print "Emp id 102 is", $2}' employee.txt

Chr9. awk 内建变量

  1. FS – 输入文件分隔符
  2. OFS – 输出文件分隔符
# 以下两个命令结果一致
awk -F ',' '{print $2, $3}' employee.txt
awk 'BEGIN {FS=","} {print $2, $3}' employee.txt
# 要实现多个命令时要用;符,然后换行时\后面不要加空格
awk 'BEGIN { FS=","; \
print "-------------\nName\tTitle\n-------------" } \
{ print $2,"\t",$3; } \
END {print "-------------"}' employee.txt
#实现多个分隔符对输入文件分割
$ awk 'BEGIN {FS="[,:%]"} {print $2, $3}' employee-multiple-fs.txt

$ awk -F ',' 'BEGIN { OFS=":" } \
{ print $2, $3 }' employee.txt
  1. RS – 使用分隔符对输入文件重新定义每一行
  2. ORS – 使用分隔符对输出文件重新定义每一行
$ 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
  1. NR – Number of Records (行号)
  2. FILENAME – 当前文件名
  3. 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
  1. 比较运算符
> 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++; 
} 
}' 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值