博客首页 注册 建议与交流 排行榜 加入友情链接
推荐 投诉 搜索: 帮助

h0ng123 linux,oracle.unix

---在蓝天白云下自由飞翔 爱生活,爱CU---
Google


学习shell脚本编程的好地方
   h0ng123.cublog.cn
关于作者  
姓名:h0ng
职业:计算机
年龄:24
位置:广东
个性介绍:学习网络技术,UNIX/linux 系统管理.娱乐


我的分类  




6.7. Patterns and Actions

6.7. Patterns and Actions

6.7.1 Patterns

Awk patterns control what actions awk will take on a line of input. A pattern consists of a regular expression, an expression resulting in a true or false condition, or a combination of these. The default action is to print each line where the expression results in a true condition. When reading a pattern expression, there is an implied if statement. When an if is implied, there can be no curly braces surrounding it. When the if is explicit, it becomes an action statement and the syntax is different. (See "Conditional Statements" on page 227.)
Example 6.19.
% cat employees

Tom Jones       4424     5/12/66     543354

Mary Adams      5346     11/4/63     28765

Sally Chang     1654     7/22/54     650000

Billy Black     1683     9/23/44     336500





(The Command Line)

1   nawk '/Tom/' employees

    Tom Jones      4424      5/12/66      543354



2   nawk '$3 < 4000' employees

    Sally Chang      1654      7/22/54      650000

    Billy Black      1683      9/23/44      336500


EXPLANATION
  1. If the pattern Tom is matched in the input file, the record is printed. The default action is to print the line if no explicit action is specified. This is equivalent to
    nawk '$0 ~ /Tom/{print $0}' employees
    
    

  2. If the third field is less than 4000, the record is printed.

6.7.2 Actions

Actions are statements enclosed within curly braces and separated by semicolons.[2] If a pattern precedes an action, the pattern dictates when the action will be performed. Actions can be simple statements or complex groups of statements. Statements are separated by semicolons, or by a newline if placed on their own line.
[2] On some versions of awk, actions must be separated by semicolons or newlines, and the statements within the curly braces also must be separated by semicolons or newlines. SVR4's nawk requires the use of semicolons or newlines to separate statements within an action, but does not require the use of semicolons to separate actions; for example, the two actions that follow do not need a semicolon: nawk '/Tom/{print "hi Tom"};{x=5}' file
FORMAT
{ action }


Example 6.20.
{ print $1, $2 }


EXPLANATION
The action is to print fields 1 and 2.
Patterns can be associated with actions. Remember, actions are statements enclosed in curly braces. A pattern controls the action from the first open curly brace to the first closing curly brace. If an action follows a pattern, the first opening curly brace must be on the same line as the pattern. Patterns are never enclosed in curly braces.
FORMAT
pattern{ action statement; action statement; etc. }

     or

pattern{

     action statement

     action statement

}


Example 6.21.
% nawk '/Tom/{print "Hello there, "  $1}' employees

Hello there, Tom


EXPLANATION
If the record contains the pattern Tom, the string Hello there, Tom will print.
A pattern with no action displays all lines matching the pattern. String-matching patterns contain regular expressions enclosed in forward slashes.
 
 
 

 原文地址 about:blank
 发表于: 2006-11-28,修改于: 2006-11-28 20:21 已浏览849次,有评论0条 推荐 投诉

  网友评论

  发表评论



Copyright © 2001-2006 ChinaUnix.net All Rights Reserved

感谢所有关心和支持过ChinaUnix的朋友们
页面生成时间:0.02434

京ICP证041476号