awk工具

awk命令
awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大。简单来说awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再进行各种分析处理。

[root@localhost sed]# head -n2 test.txt |awk -F ':' '{print $1}'    以冒号分隔 打印第一段
root
bin
[root@localhost sed]# head -n2 test.txt |awk -F ':' '{print $0}'    $0 表示所有内容
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
[root@localhost sed]# head -n2 test.txt |awk -F ':' '{print $1 "#"$2"#"$3"#"$4}'
root#x#0#0        #把冒号替换成#
bin#x#1#1
[root@localhost sed]# head -n2 test.txt |awk '/oo/'   显示包含oo的行
root:x:0:0:root:/root:/bin/bash
[root@localhost sed]# head -n2 test.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
[root@localhost sed]# head -n2 test.txt |awk -F ':' '$1 ~/oo/'
root:x:0:0:root:/root:/bin/bash
[root@localhost sed]# awk -F ':' '/root/ {print $1,$3} /user/ {print $1,$3}' test.txt
root 0
operator 11
user 1000
[root@localhost sed]# awk -F ':' '$3=="0"' test.txt  第三段==0的字符
root:x:0:0:root:/root:/bin/bash
[root@localhost sed]# awk -F ':' '$3==1000' test.txt    第三段的值==1000
user:x:1000:1000:user:/home/user:/bin/bash
[root@localhost sed]# head -n3 test.txt|awk -F ':' '$7!="/sbin/nologin"' 
root:x:0:0:root:/root:/bin/bash
[root@localhost sed]# head -n3 test.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
[root@localhost sed]# head -n10 test.txt|awk -F ':' '$3>"5" && $3<"7"'    && 表示且
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
[root@localhost sed]# awk -F ':' '$3<1000 || $7=="bin/bash"' test.txt |head -n2
root:x:0:0:root:/root:/bin/bash            #|| 表示或者
bin:x:1:1:bin:/bin:/sbin/nologin
[root@localhost sed]# head -n5 test.txt |awk -F ':' '{OFS="#"} {print $1,$2,$3}'
root#x#0                        #冒号分隔改成显示 #
bin#x#1
daemon#x#2
adm#x#3
lp#x#4
[root@localhost sed]# awk -F ':' '{OFS="#"} {if ($3<1) {print $1,$2,$3}}' test.txt
root#x#0    #写全的格式 OFS是内部变量 还有NR 行 NF 段
[root@localhost sed]# awk -F ':' '{(tot=toot+$3)};END {print tot}' test.txt
1000

转载于:https://blog.51cto.com/3622288/2043333

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值