Chinaunix首页 | 论坛 | 博客
  • 博客访问: 29947
  • 博文数量: 31
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 330
  • 用 户 组: 普通用户
  • 注册时间: 2015-01-12 10:38
文章分类

全部博文(31)

文章存档

2015年(31)

我的朋友

分类: LINUX

2015-01-22 16:53:06

awk可以实现cut不能实现的功能。cut比较简单。默认分隔符是空格和TAB。

awk不仅仅是命令,也可以是编程。
awk
x>10
x>=10
x<=10

root@csc:/tmp/testdir# awk '{printf $1 "\t" $3 "\n" }' stu.txt   提取第1,3列
aaaa    bbbb

root@csc:/tmp/testdir# df -h | awk '{print $1 "\t" $5 "\t" $6}'
文件系统    已用%    挂载点
/dev/sda1    7%    /
none    0%    /sys/fs/cgroup

print自动加入换行符,printf不会自动加入换行符。

提取sda1的磁盘使用空间
root@csc:/tmp/testdir# df -h | grep sda1 | awk '{print $5}' | cut -d "%" -f1
7

BEGIN 在数据处理之前,打印字符串。
root@csc:/tmp/testdir# awk 'BEGIN{print "testBEGIN"} {print $2}' stu.txt
testBEGIN
bbbb

FS 内置变量
指定冒号:为分隔符。第一行没有被处理。因为AWK,是先读入第一行数据,再读入命令FS=:,所以第一行没有被分隔。
root@csc:/tmp/testdir# awk '{FS=":"} {print $1 "\t" $3}' /etc/passwd
root:x:0:0:root:/root:/bin/bash    
daemon    1
bin    2

加入BEGIN,先读入分隔符FS=: 。
root@csc:/tmp/testdir# awk 'BEGIN{FS=":"}  {print $1 "\t" $3}' /etc/passwdroot    0
daemon    1

END, 再执行完命令,打印字符串。
root@csc:/tmp/testdir# awk 'BEGIN{FS=":"} END{print"endend"}  {print $1 "\t" $3}' /etc/passwd

关系运算符
root@csc:/tmp/testdir# awk 'BEGIN{FS=":"} $3>500 {print $1 "\t" $3}' /etc/passwd
nobody    65534
css    1000










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