Regular use awk

awk condition
1, using regular conditional ~ containing ~ not contain!
awk -F: '$ 1 ~ / root / {Print}' User output column 1 contains the root line
awk -F:! '$ 1 ~ / root / {print } 'user output column of the first row does not contain a root
awk -F:' $ 1 ~ / root / 'user writing is omitted, the effect supra!
2, numbers, strings conditions do
comparison symbols: == (equal to) = (! is not equal to)> (greater than)

= (Greater than or equal) <(less than) <= (less than or equal)

awk 'NR==1{print}' user   输出第一行
awk 'NR==1' user 	省略写法,效果同上
awk 'NR>2' user   输出大于第二行的行
awk -F: '$3<10{print}' /etc/passwd  输出第3列(uid)小于10的行
awk -F: '$3<10' /etc/passwd  效果同上
awk -F: '$3<10{print $3}' /etc/passwd  找到第3列小于10的行,并输出第3列
awk -F: '$3>=1000' /etc/passwd  输出uid大于等于1000的账号信息

3, the logic test conditions
Condition 1 Condition 2 {print} && two conditions are met before the print execution task
Condition 1 Condition 2 {print} || any executes a print task conditions are met

awk 'NR>=2&&NR<=4' user   输出2~4行
awk -F: 'NR>=2&&NR<=4{print $1}' user 找2~4行并输出第1列
awk -F: 'NR==4 ||NR==5{print $1}' user 找4行或5行并输出第1列
awk -F: 'NR>=10 || NR<=20{print $1}' user 找10行以及大于10行的内容, 或者20以内的行, 相当于所有行,再显示第1列
awk -F: 'NR<10 && NR>20{print $1}' user 不存在的行,逻辑错误

#!/bin/bash
user=awk -F: '/bash$/{print $1}' /etc/passwd
for i in $user
do
grep $i /etc/shadow | awk -F: ‘{print $1 “–>” $2}’
done

Published 14 original articles · won praise 0 · Views 200

Guess you like

Origin blog.csdn.net/nbnbnb_/article/details/103898302