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

h0ng123 linux,oracle.unix

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


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


我的分类  




6.2.1 Input from Files

In the following examples, the percent sign is a shell prompt. Please take note: In all examples where the command is nawk, use either awk or gawk if you are on an HP-UX flavor system or using Linux.

FORMAT

% nawk 'pattern' filename

% nawk '{action}' filename

% nawk 'pattern {action}' filename


Here is a sample file called employees:

Example 6.1.
% 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



% nawk '/Mary/'  employees

Mary Adams            5346    11/4/63                28765


EXPLANATION

Nawk prints all lines that contain the pattern Mary.

Example 6.2.
% 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



% nawk '{print $1}' employees

Tom

Mary

Sally

Billy


EXPLANATION

Nawk prints the first field of file employees, where the field starts at the left margin of the line and is delimited by whitespace.

Example 6.3.
% 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



% nawk '/Sally/{print $1, $2}' employees

Sally Chang


EXPLANATION

Nawk prints the first and second fields of file employees, only if the line contains the pattern Sally. Remember, the field separator is whitespace.

6.2.2 Input from Commands

The output from a UNIX/Linux command or commands can be piped to awk for processing. Shell programs commonly use awk for manipulating commands.

FORMAT

% command | nawk 'pattern'

% command | nawk '{action}'

% command | nawk 'pattern {action}'


Example 6.4.
1   % df | nawk '$4 > 75000'

    /oracle   (/dev/dsk/c0t0d057 ):390780 blocks     105756 files

    /opt    (/dev/dsk/c0t0d058 ):1943994 blocks      49187 files



2   % rusers  | nawk '/root$/{print  $1}'

    owl

    crow

    bluebird


EXPLANATION

  1. The df command reports the free disk space on file systems. The output of the df command is piped to nawk. If the fourth field is greater than 75,000 blocks, the line is printed.

  2. The rusers command prints those logged on remote machines on the network. The output of the rusers command is piped to nawk as input. The first field is printed if the regular expression root is matched at the end of the line ($); that is, all machine names are printed where root is logged on.

 发表于: 2006-11-19,修改于: 2006-11-28 19:54 已浏览947次,有评论0条 推荐 投诉

  网友评论

  发表评论



Copyright © 2001-2006 ChinaUnix.net All Rights Reserved

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

京ICP证041476号