Chinaunix首页 | 论坛 | 博客
  • 博客访问: 539001
  • 博文数量: 65
  • 博客积分: 1158
  • 博客等级: 少尉
  • 技术积分: 1261
  • 用 户 组: 普通用户
  • 注册时间: 2012-07-18 22:07
文章分类

全部博文(65)

文章存档

2016年(1)

2014年(2)

2013年(9)

2012年(53)

分类: LINUX

2012-10-26 10:33:47

1.NF:支持记录域个数,在记录被读之后再设置 1

[root@redhat wilsontest]# pwd

/tmp/wilsontest

[root@redhat wilsontest]# echo $PWD | awk -F / '{print $NF}'

wilsontest

这里取得的是当前目录的最后一个文件的名字。

再如:

[root@redhat wilsontest]# echo "/usr/local/etc/rc.sybase" | awk -F / '{print $NF}'

rc.sybase

-F /表示以’/’分开域

2.NR:已经读取的记录数 3.FILENAME

定义:支持a w k脚本实际操作的输入文件。因为a w k可以同时处理许多文件,因此如果访问了这个变量,将告之系统目前正在浏览的实际文件。

 

例如:

[root@redhat wilsontest]# cat filetest

hello wilson

world

linux

oracle

C++

 

[root@redhat wilsontest]# awk '{print NF,NR,$0}END{print FILENAME}' filetest

2 1 hello wilson

1 2 world

1 3 linux

1 4 oracle

1 5 C++

说明:第一列NF输出读取记录的域的个数;NR表示已经读取的记录数;$0实际就是把记录输出出来;FILENAME就是输出正在处理的文件。

为了说明FILENAME的作用,再做如下操作:

[root@redhat wilsontest]# cp filetest filetest.bak

[root@redhat wilsontest]# ls -la

total 28

drwxr-xr-x  2 root root 4096 Oct 26 10:02 .

drwxrwxrwt 11 root root 4096 Oct 26 09:46 ..

-rw-r--r--  1 root root   36 Oct 26 10:00 filetest

-rw-r--r--  1 root root   36 Oct 26 10:02 filetest.bak

-rw-r--r--  1 root root 4617 Oct 23 09:10 man.config

[root@redhat wilsontest]# awk '{print NF,NR,$0}END{print FILENAME}' filetest filetest.bak

2 1 hello wilson

1 2 world

1 3 linux

1 4 oracle

1 5 C++

2 6 hello wilson

1 7 world

1 8 linux

1 9 oracle

1 10 C++

filetest.bak

很明显的,是先处理filetest文件,然后再处理filetest.bak文件,处理完之后输出FILENAME就是最后处理的那个filetest.bak文件;这里还得注意的是,处理的记录数NR是不断叠加的。

 

 

阅读(3993) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~