注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

随机过程

http://superware.blog.163.com

 
 
 

日志

 
 

Patterns in AWK  

2015-01-16 16:38:49|  分类: 默认分类 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

1. BEGIN and END Pattern

BEGIN {
printf ("%-9s %-9s %-9s %-9s\n", "COUNTRY", "AREA", "POPU", "CONTINENT")
printf ("\n")
}
{
print
}
END {
printf ("\nEND.\n")
}


2. Expression  Pattern

BEGIN {
printf ("%-9s %-9s %-9s %-9s\n", "COUNTRY", "AREA", "POPU", "CONTINENT")
printf ("\n")
}

$2 > 1000 && $2 < 4000 {
print
}

END {
printf ("\nEND.\n")
}
The bold part is an expression pattern.

3. String-Matching Patterns

AWK provides a notation called regular expression for specifying and matching strings of characters. Regular expressions are widely used in Unix programs, including its text editors and shell.

A string-matching pattern tests whether a string contains a substring matched by a regular expression.

The simplest regular expression is a string of letters and numbers, like Asia, that matches itself.

A regular expression is not a string-matching pattern. To turn a regular expression into a string-matching pattern, just enclose it in slashes:

/Asia/

This pattern matches when the current input line contains the substring Asia, either as Asia by itself or as some part of a large word like Asian or Pan-Asiatic, and so on.

Note that blanks are significant within the string-matching pattern. The string-matching pattern

/ Asia /

matches only when Asia is surrounded by blanks.

========================================================================
                                          String-Matching Patterns
1. /regexpr/
   
Matches when the current input line contains a substring matched by regexpr

2. expression ~/regexpr/
   
Matches if the string value of expression contains a substring matched by regexpr

3. expression !~/regexpr/
    Matches if the string value of expression does not contain a substring matched by regexpr

========================================================================

BEGIN {
printf ("%-9s %-9s %-9s %-9s\n", "COUNTRY", "AREA", "POPU", "CONTINENT")
printf ("\n")
}

$5 ~ /America/ {
print
}

END {
printf ("\nEND.\n")
}

The string 

$5 ~ /America/

matches the substring of America in field $5. and
BEGIN {
printf ("%-9s %-9s %-9s %-9s\n", "COUNTRY", "AREA", "POPU", "CONTINENT")
printf ("\n")
}

$5 !~ /America/ {
print
}

END {
printf ("\nEND.\n")
}
the string
$5 !~ /America/

matches the line whoes field $5 not include the substring America.
  评论这张
 
阅读(83)| 评论(0)
推荐

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017