shell训练day5 8.19
AWK
[[email?protected] awk]# awk -F ‘:‘ ‘{print $1}‘ test.txt $1 表示第一段,$0 表示全部 ,如果没有指定分隔符,使用空格作为分隔符。 [[email?protected] awk]# awk ‘{print $1}‘ 1.txt [[email?protected] awk]# awk -F ‘:‘ ‘{print $1,$3,$4}‘ test.txt [[email?protected] awk]# awk -F ‘:‘ ‘{print $1"#"$3"#"$4}‘ test.txt 使用#来替换空格 [[email?protected] awk]# awk ‘/oo/‘ test.txt --打印带oo的 [[email?protected] awk]# awk -F ‘:‘ ‘$1~/oo/‘ test.txt --打印第一段带oo的行 [[email?protected] awk]# awk -F ‘:‘ ‘/root/ {print $1,$3} /user/ {print $1,$4}‘ test.txt [[email?protected] awk]# awk -F ‘:‘ ‘/root|user/ {print $0}‘ test.txt [[email?protected] awk]# awk -F ‘:‘ ‘$3==0 {print $0}‘ test.txt [[email?protected] awk]# awk -F ‘:‘ ‘$3>=1000 {print $0}‘ test.txt ---注意这里的1000如果是数字就不应该加"" awk -F ‘:‘ ‘$7!="/sbin/nologin"‘ /etc/passwd awk -F ‘:‘ ‘$7!="/sbin/nologin"‘ /etc/passwd [[email?protected] awk]# awk -F ‘:‘ ‘$3<$4 {print$3,$4}‘ test.txt [[email?protected] awk]# awk -F ‘:‘ ‘$3>1000 || $7=="/bin/bash"‘ /etc/passwd head -5 /etc/passwd |awk -F ‘:‘ ‘{OFS="#"} {print $1,$4}‘ [[email?protected] system]# head -5 /etc/passwd |awk -F ‘:‘ ‘{OFS="#"} {print $1,$4}‘ 可以带if NR表示行 NF表示段 awk -F ‘:‘ ‘{print NR":" $0}‘ test.txt awk -F ‘:‘ ‘{print NF":" $0}‘ test.txt awk -F ‘:‘ ‘NR<=10‘ test.txt [[email?protected] awk]# awk -F ‘:‘ ‘NR<=10‘ test.txt awk -F ‘:‘ ‘NR<=10 && $1 ~/root|sync/‘ test.txt ~表示包含 awk -F ‘:‘ ‘NF==6 && $1 ~/root|sync/‘ test.txt head -n 3 /etc/passwd |awk -F ‘:‘ ‘$1="root"‘ head -n 3 test.txt |awk -F ‘:‘ ‘{OFS=":"} $1="root"‘ awk -F ‘:‘ ‘{(tot=tot+$3)}; END {print tot}‘ /etc/passwd ---求和一列 awk -F ‘:‘ ‘{if ($1=="root") {print $0}}‘ /etc/passwd (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |