首页 > 其他 > 详细

awk

时间:2017-04-07 19:07:14      阅读:165      评论:0      收藏:0      [点我收藏+]

awk:一门语言,过滤内容(取列),打印,删除

awk ‘{print $1}‘ 文件

例如,文件如下:

[root@wuyike ~]# cat awk.txt

wuyike wuyikekeke

ddd fff

eded


显示文件的第一列:

[root@wuyike ~]# awk ‘{print $1}‘ awk.txt

wuyike

ddd

eded


显示文件的第二列

[root@wuyike ~]# awk ‘{print $2}‘ awk.txt

wuyikekeke

fff


显示两列:

[root@wuyike ~]# awk ‘{print $1 $2}‘ awk.txt

wuyikewuyikekeke

dddfff

eded


显示两列并加分隔符:

[root@wuyike ~]# awk ‘{print $1" "$2" "}‘ awk.txt

wuyike wuyikekeke 

ddd fff 

eded 


[root@wuyike ~]# less /etc/passwd

root:x:0:0:root:/root:/bin/bash

bin:x:1:1:bin:/bin:/sbin/nologin

daemon:x:2:2:daemon:/sbin:/sbin/nologin

adm:x:3:4:adm:/var/adm:/sbin/nologin

lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

sync:x:5:0:sync:/sbin:/bin/sync


显示以冒号为分隔符的第一列:

[root@wuyike ~]# awk -F ":" ‘{print $1}‘ /etc/passwd

root

bin

daemon

adm

lp

sync


显示以分号为分隔符的最后一列:(NF是指最后一列)

[root@wuyike ~]# awk -F "/" ‘{print $NF}‘ /etc/passwd

bash

nologin

nologin

nologin

nologin

sync

shutdown

halt


注:Linux里面严格区分大小写


NR代表行号

[root@wuyike ~]# awk ‘{if(NR<31 && NR>19) print $1"\n"}‘ test.txt

20


21


22


23


24


25


26


27


28


29


30






awk

原文:http://11815879.blog.51cto.com/11805879/1913742

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!