*和+的区别:
test\w* :*表示0个或多个字段 单词
test\w+ : +表示test后面有1个或多个字母
awk中~的意思
[ck143117137@shell.ceshiren.com ~]$ less test03.txt | awk '$2~/am/'
I am a tester, and My job is to test some software.
##第2列模糊匹配到am字符
多个条件同时匹配
[ck143117137@shell.ceshiren.com ~]$ awk '/oo/&&/user/{print $0}' test03.txt
hahahaa usernamoroot
[ck143117137@shell.ceshiren.com ~]$ awk '/oo/ {print $1} /user/{print $0}' test03.txt
I #/oo/ {print $1} 打印出匹配到oo的第一列
hahahaa # /user/{print $0} 打印匹配到user的所有内容
hahahaa usernamoroot
打印出某段不等于xx的行
[ck143117137@shell.ceshiren.com ~]$ cat test03.txt | awk '$2!="/sbin/nologin" {print $2}'
## 匹配第二列不等于/sbin/nologin的行 打印该行的第二列
//字符作为判断条件则是要使用双引号括起来的
am
usernamoroot
变量NR的用法 表示行号
[ck143117137@shell.ceshiren.com ~]$ cat test03.txt | awk '$2!="/sbin/nologin" {print NR":" $2}'
1:am
2:usernamoroot
3:
变量的应用
[ck143117137@shell.ceshiren.com ~]$ cat test03.txt | awk '{print $NR}'
I
usernamoroot
san
si
wu
## 当时看打印出来一脸懵逼,心里想为什么会这样打印呢,慢慢进行分析 首先单独打印NR,NR是1,2,3,4,5 知道了NR的值,$NR 就等于第一行的$1,第二行$2,第三行$3,第四行$4,第五行$5。 这样就想通了,NF同理
[ck143117137@shell.ceshiren.com ~]$ cat test03.txt | awk '{print NR}'
1
2
3
4
5
练习1
练习2
练习3
实战练习
练习1:
练习2:
练习3
答案1:
答案2
答案3