linux系統中awk命令 正則匹配


 

1、測試數據

[root@centos7 test3]# cat b.txt
e t s e
s g m x
w d g i
d t e g
x g e w

 

2、打印匹配w的行

[root@centos7 test3]# cat b.txt e t s e s g m x w d g i d t e g x g e w [root@centos7 test3]# awk '/w/' b.txt w d g i x g e w [root@centos7 test3]# awk '/w/ {print $1, $3}' b.txt w g x e

 

3、打印第一列匹配w的行

[root@centos7 test3]# cat b.txt e t s e s g m x w d g i d t e g x g e w [root@centos7 test3]# awk '$1 ~ /w/' b.txt w d g i [root@centos7 test3]# awk '$1 ~ /w/ {print $1,$4}' b.txt w i

 

4、打印全文沒有匹配w的行

[root@centos7 test3]# cat b.txt e t s e s g m x w d g i d t e g x g e w [root@centos7 test3]# awk '!/w/' b.txt e t s e s g m x d t e g

 

5、打印第一列沒有匹配w的行

[root@centos7 test3]# cat b.txt e t s e s g m x w d g i d t e g x g e w [root@centos7 test3]# awk '$1 !~ /w/' b.txt e t s e s g m x d t e g x g e w

 

6、打印同時匹配s或者w的行

[root@centos7 test3]# cat b.txt e t s e s g m x w d g i d t e g x g e w [root@centos7 test3]# awk '/[sw]/' b.txt e t s e s g m x w d g i x g e w

 

7、打印第3列匹配e同時第四列匹配g的行

[root@centos7 test3]# cat b.txt e t s e s g m x w d g i d t e g x g e w [root@centos7 test3]# awk '$3 ~ /e/' b.txt d t e g x g e w [root@centos7 test3]# awk '$3 ~ /e/ && $4 ~ /g/' b.txt d t e g

 

8、輸出第3列沒有匹配e的行

[root@centos7 test3]# cat b.txt e t s e s g m x w d g i d t e g x g e w [root@centos7 test3]# awk '$3 !~ /e/' b.txt e t s e s g m x w d g i

 

9、匹配包含兩個關鍵詞中間的所有行

[root@centos7 test3]# cat c.txt 1 張三 歷史 81 B 0.367
2 李四 物理 72 C 0.588
3 李華 數學 87 B+ 0.677
4 方咪 歷史 91 A 0.876
5 陳明 語文 81 B 0.812
6 魚魚 英語 81 B 0.571
1 張三 歷史 81 B 0.367
2 李四 物理 72 C 0.588
3 李華 數學 87 B+ 0.677
4 方咪 歷史 91 A 0.876
5 陳明 語文 81 B 0.812
6 魚魚 英語 81 B 0.571 [root@centos7 test3]# awk '/李四/,/陳明/' c.txt 2 李四 物理 72 C 0.588
3 李華 數學 87 B+ 0.677
4 方咪 歷史 91 A 0.876
5 陳明 語文 81 B 0.812
2 李四 物理 72 C 0.588
3 李華 數學 87 B+ 0.677
4 方咪 歷史 91 A 0.876
5 陳明 語文 81 B 0.812

 

[root@centos7 test3]# cat a.txt 1 張三 歷史 81 B 0.367
2 李四 物理 72 C 0.588
3 李華 數學 87 B+ 0.677
4 方咪 歷史 91 A 0.876
5 陳明 語文 81 B 0.812
6 魚魚 英語 81 B 0.571 [root@centos7 test3]# awk '$4==72,$4==91 {print $0}' a.txt 2 李四 物理 72 C 0.588
3 李華 數學 87 B+ 0.677
4 方咪 歷史 91 A 0.876

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM