|
awk命令
awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大。简单来说awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再进行各种分析处理。
[iyunv@localhost sed]# head -n2 test.txt |awk -F ':' '{print $1}' 以冒号分隔 打印第一段
root
bin
[iyunv@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
[iyunv@localhost sed]# head -n2 test.txt |awk -F ':' '{print $1 "#"$2"#"$3"#"$4}'
root#x#0#0 #把冒号替换成#
bin#x#1#1
[iyunv@localhost sed]# head -n2 test.txt |awk '/oo/' 显示包含oo的行
root:x:0:0:root:/root:/bin/bash
[iyunv@localhost sed]# head -n2 test.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
[iyunv@localhost sed]# head -n2 test.txt |awk -F ':' '$1 ~/oo/'
root:x:0:0:root:/root:/bin/bash
[iyunv@localhost sed]# awk -F ':' '/root/ {print $1,$3} /user/ {print $1,$3}' test.txt
root 0
operator 11
user 1000
[iyunv@localhost sed]# awk -F ':' '$3=="0"' test.txt 第三段==0的字符
root:x:0:0:root:/root:/bin/bash
[iyunv@localhost sed]# awk -F ':' '$3==1000' test.txt 第三段的值==1000
user:x:1000:1000:user:/home/user:/bin/bash
[iyunv@localhost sed]# head -n3 test.txt|awk -F ':' '$7!="/sbin/nologin"'
root:x:0:0:root:/root:/bin/bash
[iyunv@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
[iyunv@localhost sed]# head -n10 test.txt|awk -F ':' '$3>"5" && $3<"7"' && 表示且
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
[iyunv@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
[iyunv@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
[iyunv@localhost sed]# awk -F ':' '{OFS="#"} {if ($3<1) {print $1,$2,$3}}' test.txt
root#x#0 #写全的格式 OFS是内部变量 还有NR 行 NF 段
[iyunv@localhost sed]# awk -F ':' '{(tot=toot+$3)};END {print tot}' test.txt
1000
|
|
|