2018-03-14 Linux学习
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2018-03-14 Linux学习相关的知识,希望对你有一定的参考价值。
9.6 awk (上)
awk 示例
head -n2 test.txt|awk -F ‘:‘ ‘{print $1}‘
head -n2 test.txt|awk -F ‘:‘ ‘{print $0}‘
awk -F ‘:‘ ‘{print $1"#"$2"#"$3"#"$4}‘
awk ‘/oo/‘ test.txt
awk -F ‘:‘ ‘$1 ~ /oo/‘ test.txt
awk -F ‘:‘ ‘/root/ {print $1,$3} /test/ {print $1,$3}‘ test.txt
awk -F ‘:‘ ‘$3=="0"‘ /etc/passwd
awk -F ‘:‘ ‘$3>="500"‘ /etc/passwd
awk -F ‘:‘ ‘$3>=500‘ /etc/passwd
awk -F ‘:‘ ‘$7!="/sbin/nologin"‘ /etc/passwd
-F 以...为分隔符
无需加脱义字符
数学运算时,字符串需要加上双引号
[[email protected] awk]# head -2 text.txt|awk -F ‘:‘ ‘{print $1}‘
root
bin
[[email protected] awk]# head -2 text.txt|awk -F ‘:‘ ‘{print $0}‘
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
[[email protected] awk]# head -2 text.txt|awk -F ‘:‘ ‘{print $1"#"$2"#"$3}‘
root#x#0
bin#x#1
[[email protected] awk]# awk ‘/roo/‘ text.txt
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[[email protected] awk]# awk -F ‘:‘ ‘$1 ~ /roo/‘ text.txt
root:x:0:0:root:/root:/bin/bash
[[email protected] awk]# awk -F ‘:‘ ‘/root/ {print $1,$3} /lp/ {print $1,$3,$4}‘ text.txt
root 0
lp 4 7
operator 11
[[email protected] awk]# awk -F ‘:‘ ‘/root|lp/ {print $1,$3}‘ text.txt
root 0
lp 4
operator 11
[[email protected] awk]# awk -F ‘:‘ ‘/root|lp/ {print $0}‘ text.txt
root:x:0:0:root:/root:/bin/bash
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
[[email protected] awk]# awk -F ‘:‘ ‘$3==0 {print $1}‘ text.txt
root
[[email protected] awk]# awk -F ‘:‘ ‘$3>=500 {print $0}‘ text.txt
polkitd:x:999:997:User for polkitd:/:/sbin/nologin
[[email protected] awk]# awk -F ‘:‘ ‘$7!="/sbin/nologin" {print $0}‘ text.txt
root:x:0:0:root:/root:/bin/bash
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
9.7 awk (下)
OFS 打印时指定的分隔符
NR 行
NF 段
示例
awk -F ‘:‘ ‘$3<$4‘ /etc/passwd
awk -F ‘:‘ ‘$3>"5" && $3<"7"‘ /etc/passwd
awk -F ‘:‘ ‘$3>1000 || $7=="/bin/bash"‘ /etc/passwd
head -5 /etc/passwd | awk -F ‘:‘ ‘{OFS="#"} {print $1,$3,$4}‘
awk -F ‘:‘ ‘{OFS="#"} {if ($3>500) {print $1,$2,$3,$4}}‘ /etc/passwd
head -3 /etc/passwd | awk -F ‘:‘ ‘{print NF}‘
head -3 /etc/passwd | awk -F ‘:‘ ‘{print NR}‘
awk ‘NR>40‘ /etc/passwd
awk -F ‘:‘ ‘NR<20 && $1 ~ /roo/‘ /etc/passwd
head -n 3 /etc/passwd | awk -F ‘:‘ ‘$1="root"‘
awk -F ‘:‘ ‘{(tot=tot+$3)}; END {print tot}‘ /etc/passwd
awk -F ‘:‘ ‘{if($1=="root") {print $0}}‘ /etc/passwd
[[email protected] awk]# awk -F ‘:‘ ‘$3<$4‘ text.txt
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
[[email protected] awk]# awk -F ‘:‘ ‘$3>5 && $3<7‘ text.txt
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
[[email protected] awk]# awk -F ‘:‘ ‘$3<5 && $7 ~ /bash/‘ text.txt
root:x:0:0:root:/root:/bin/bash
[[email protected] awk]# awk -F ‘:‘ ‘{OFS="#"} $3>500 || $7 ~ /bash/ {print $1,$3,$7}‘ text.txt
root#0#/bin/bash
polkitd#999#/sbin/nologin
[[email protected] awk]# awk -F ‘:‘ ‘{OFS="#"} {if($3>500 && $7 ~ /nologin/) {print $1,$3,$7}}‘ text.txt
polkitd#999#/sbin/nologin
[[email protected] awk]# awk -F ‘:‘ ‘NR<=3‘ text.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
[[email protected] awk]# awk -F ‘:‘ ‘NR<=10 && $1 ~ /root|sync/‘ text.txt
root:x:0:0:root:/root:/bin/bash
sync:x:5:0:sync:/sbin:/bin/sync
[[email protected] awk]# head -3 text.txt | awk -F ‘:‘ ‘$1="root"‘
root x 0 0 root /root /bin/bash
root x 1 1 bin /bin /sbin/nologin
root x 2 2 daemon /sbin /sbin/nologin
[[email protected] awk]# awk -F ‘:‘ ‘{(tot=tot+$3)}; END {print tot}‘ text.txt
1788
[[email protected] awk]# awk -F ‘:‘ ‘{if($1=="root") {print $0}}‘ text.txt
root:x:0:0:root:/root:/bin/bash
以上是关于2018-03-14 Linux学习的主要内容,如果未能解决你的问题,请参考以下文章