Chinaunix首页 | 论坛 | 博客
  • 博客访问: 658428
  • 博文数量: 125
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 962
  • 用 户 组: 普通用户
  • 注册时间: 2012-10-14 10:20
个人简介

我是一只小白兔~

文章分类

全部博文(125)

文章存档

2017年(16)

2016年(108)

2014年(1)

我的朋友

分类: LINUX

2016-11-30 09:24:15

一、awk简介
 1.awk是3个姓氏的首字母,代表该语言的3个作者,awk的版本有很多,包括:旧版awk,新版awk(nawk),GNU awk(gawk)等。
   awk程序有awk命令,括在引号或写在文件中的指令以及输入文件这几个部分组成。
 
 2.检查系统中是否安装有awk

 [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

注:以后的例子都是采用gawk命令

AWK简介及使用实例 

AWK 简介和例子 

Shell脚本之AWK文本编辑器语法 

正则表达式中AWK的学习和使用 

文本数据处理之AWK 图解 


二、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]# gawk '{ print $1,$3 }' names.txt  

Tom    100
Molly  200
John   300
 

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

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


三、从文件输入
 1.格式:
 gawk  '/匹配字符串/'
 gawk  '{处理动作}'
 gawk  '/ 匹配字符串/ {处理动作}' 文件名

 2. 使用awk查找文件中包含root的行
 [root@rhel helinbash]# gawk '/root/' /etc/passwd
 root:x:0:0:root:/root:/bin/bash
 operator:x:11:0:operator:/root:/sbin/nologin

#使用grep命令查找

 [root@rhel helinbash]# grep root /etc/passwd
 root:x:0:0:root:/root:/bin/bash
 operator:x:11:0:operator:/root:/sbin/nologin


3.使用gawk命令查找以root开头的行
 [root@rhel helinbash]# gawk '/^root/' /etc/passwd
 root:x:0:0:root:/root:/bin/bash

4.
 [root@rhel helinbash]# gawk '/^root/'
 root
 root
 root
 root
 studnet
 t^H^[[3~^H^H^H this is a demo string wih^H^H iclcude root key woard
 
root hello abc
root hello abc
 
注:红色的字体是过滤后的输出,这个是gawk的交互式执行命令

 5. 以冒号为分隔符,打印第1列和第3列的数据,两列之间用一个空格分隔
 [root@rhel helinbash]# gawk -F: '{ print $1,$3 }' /etc/passwd
 root 0
 bin 1
 daemon 2
 adm 3
 lp 4
 sync 5
 shutdown 6
 halt 7
 mail 8
 news 9
 uucp 10
 operator 11
 games 12
 gopher 13
 ftp 14
 nobody 99
 nscd 28
 vcsa 69
 rpc 32
 mailnull 47
 smmsp 51
 pcap 77
 ntp 38
 dbus 81
 avahi 70
 sshd 74
 rpcuser 29
 nfsnobody 65534
 haldaemon 68
 avahi-autoipd 100
 xfs 43
 gdm 42
 sabayon 86
  500
 named 25

6. 查看包含root行的行,并打印这些行的第1列和第3列
 [root@rhel helinbash]# gawk -F: '/root/{ print $1 $3 }' /etc/passwd  
 root0
 operator11


7.
 格式化输出print函数
 awk命令操作处理部分是放在“{}”(括号)中;print函数将变量和字符夹杂着输出。 如同linux中的echo命令
 
、从命令输入

 1.awk还可以处理通过管道接收到的linux命令的结果,shell程序通常使用awk做深处理。
 
(1)
 格式:
 命令| gawk '/匹配字符串/'
 命令| gawk '{处理动作}'
 命令| gawk '/匹配字符串/ {处理动作}'
 (2)举例
 [root@rhel helinbash]# date
 Mon May 26 10:10:01 CST 2014
 
[root@rhel helinbash]# date | gawk '{print "Month:"$2"\nYear:"$1 }' 

Month:May
Year:2014

[root@rhel helinbash]# date | gawk '{print "Month:",$2"\nYear:",$1 }' 
Month: May
Year: 2014


注意:如果在上面添加了逗号的话,那么打印结果的冒号后会多一个空格作为分隔符。

(3)注意上面的例子,一种是直接在Month后连接$2,另一种是在Year和$6之间使用了逗号,都由OFS决定。
阅读(1420) | 评论(0) | 转发(0) |
0

上一篇:Sed命令

下一篇:网址导航[IT]

给主人留下些什么吧!~~