AWK原理及命令和文件输入

<

div id=”content” contentScore=”2205″>一,awk简介
 1,
 awk是3个姓氏的首字母,代表该语言的3个作者,awk的版本有很多,包括:旧版awk,新版awk(nawk),GNU awk(gawk)等。
 awk程序有awk命令,括在引号或写在文件中的指令以及输入文件这几个部分组成。
 2,
 [root@rhel helinbash]# which awk
 /bin/awk
 [root@rhel helinbash]# which gawk
 /bin/gawk
 [root@rhel helinbash]# ls -l /bin/awk /bin/gawk
 lrwxrwxrwx 1 root root      4 Oct 10  2013 /bin/awk -> gawk
 -rwxr-xr-x 1 root root 320416 Jan 15  2007 /bin/gawk
 [root@rhel helinbash]#
注:以后的例子都是采用gawk命令

AWK简介及使用实例 http://www.linuxidc.com/Linux/2013-12/93519.htm

AWK 简介和例子 http://www.linuxidc.com/Linux/2012-12/75441.htm

Shell脚本之AWK文本编辑器语法 http://www.linuxidc.com/Linux/2013-11/92787.htm

正则表达式中AWK的学习和使用 http://www.linuxidc.com/Linux/2013-10/91892.htm

文本数据处理之AWK 图解 http://www.linuxidc.com/Linux/2013-09/89589.htm

二,awk工作原理
 1,
 以下内容的names文件名举例按步骤解析awk的处理过程
 (1)
 vim names
 Tom Savage 100
 Molly Lee 200
 John Doe 300
 (2)
 [root@rhel helinbash]# cat names.txt | cut -d  ‘ ‘ -f 2
 

Savage
        Lee
 

[root@rhel helinbash]# cat names.txt | cut -d  ‘t’ -f 2 
 cut: the delimiter must be a single character
 Try `cut –help’ for more information.
 [root@rhel helinbash]#
(3)
 [root@rhel helinbash]# gawk ‘{ print $1,$3 }’ names.txt
 
 
 Tom 100
 Molly 200
 John 300
 
 [root@rhel helinbash]#

[root@rhel helinbash]# gawk ‘{ print $1        $3 }’ names.txt 
 

Tom100
 Molly200
 John300
 
[root@rhel helinbash]#

2,
 原理图
 FS:Field separator(分隔符)
 OFS:Output Field Separator

第三步:awk中print命令打印字段;{print $1,$3} 只取有用的第一段和第三段;在打印时$1和$3之间由空格间隔。“,”逗号是一个映射到内部的输出字段分隔符(OFS),OFS变量
 缺省为空格,逗号在输出时被空格替换。接下来,awk处理下一行数据,直到所有的行处理完。

更夼/div>