shell编程,其实主要是学会sed和awk。今天集中精力看看awk,做一下笔记,顺便更新一下自己的博客。
coin.txt的内容:
metal weight in ounces date minted country of origin description
gold 1 1986 USA American Eagle
gold 1 1908 Austria-Hungary Franz Josef 100 Korona
silver 10 1981 USA ingot
gold 1 1984 Switzerland ingot
gold 1 1979 RSA Krugerrand
gold 0.5 1981 RSA Krugerrand
gold 0.1 1986 PRC Panda
silver 1 1986 USA Liberty dollar
gold 0.25 1986 USA Liberty 5-dollar piece
silver 0.5 1986 USA Liberty 50-cent piece
silver 1 1987 USA Constitution dollar
gold 0.25 1987 USA Constitution 5-dollar piece
gold 1 1988 Canada Maple Leaf
1)检索特定的字符串
[/tmp]# awk '/gold/' coins.txt
gold 1 1986 USA American Eagle
gold 1 1908 Austria-Hungary Franz Josef 100 Korona
gold 1 1984 Switzerland ingot
gold 1 1979 RSA Krugerrand
gold 0.5 1981 RSA Krugerrand
gold 0.1 1986 PRC Panda
gold 0.25 1986 USA Liberty 5-dollar piece
gold 0.25 1987 USA Constitution 5-dollar piece
gold 1 1988 Canada Maple Leaf
2)检索特定的字符串,选择最后一列
[/tmp]# awk '/gold/ {print $5 $6 $7 $8 }' coins.txt
AmericanEagle
FranzJosef100Korona
ingot
Krugerrand
Krugerrand
Panda
Liberty5-dollarpiece
Constitution5-dollarpiece
MapleLeaf
3)输出全部行
[/tmp]# awk '/gold/ {print $0}' coins.txt
gold 1 1986 USA American Eagle
gold 1 1908 Austria-Hungary Franz Josef 100 Korona
gold 1 1984 Switzerland ingot
gold 1 1979 RSA Krugerrand
gold 0.5 1981 RSA Krugerrand
gold 0.1 1986 PRC Panda
gold 0.25 1986 USA Liberty 5-dollar piece
gold 0.25 1987 USA Constitution 5-dollar piece
gold 1 1988 Canada Maple Leaf
4)使用if条件检索
[/tmp]# awk '{if ( $3 < 1980 ) print $3," ",$5,$6,$7,$8}' coins.txt
1908 Franz Josef 100 Korona
1979 Krugerrand
提示:awk的自己的命令总是放在{}中进行执行。
阅读(1152) | 评论(0) | 转发(0) |