Chinaunix首页 | 论坛 | 博客
  • 博客访问: 697986
  • 博文数量: 160
  • 博客积分: 8847
  • 博客等级: 中将
  • 技术积分: 1656
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-25 16:46
个人简介

。。。。。。。。。。。。。。。。。。。。。。

文章分类

全部博文(160)

文章存档

2015年(1)

2013年(1)

2012年(4)

2011年(26)

2010年(14)

2009年(36)

2008年(38)

2007年(39)

2006年(1)

分类: LINUX

2008-07-25 00:07:34

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的自己的命令总是放在{}中进行执行。
 
 
 
阅读(1118) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~