Linux 中awk语句匹配首个数字与匹配首个连续的数字的区别
001、
[root@localhost test]# cat b.txt ## 测试数据 0001 20081223efs333kjfdj EREADFASDLKJCV 0002 20081208djfks2288daa JDKFJALSDJFsddf 0003 20081208efskjfdj EREADFASDLKJCV 0004 20081211djfksdaa1234 JDKFJALSDJFsddf [root@localhost test]# awk '{sub("[0-9]", "", $2); print $0}' b.txt ## 匹配首个数字 0001 0081223efs333kjfdj EREADFASDLKJCV 0002 0081208djfks2288daa JDKFJALSDJFsddf 0003 0081208efskjfdj EREADFASDLKJCV 0004 0081211djfksdaa1234 JDKFJALSDJFsddf [root@localhost test]# awk '{sub("[0-9]+", "", $2); print $0}' b.txt ## 匹配首个连续的数字 0001 efs333kjfdj EREADFASDLKJCV 0002 djfks2288daa JDKFJALSDJFsddf 0003 efskjfdj EREADFASDLKJCV 0004 djfksdaa1234 JDKFJALSDJFsddf
。
002、
[root@localhost test]# cat b.txt ## 测试数据 0001 20081223efs333kjfdj EREADFASDLKJCV 0002 20081208djfks2288daa JDKFJALSDJFsddf 0003 20081208efskjfdj EREADFASDLKJCV 0004 20081211djfksdaa1234 JDKFJALSDJFsddf [root@localhost test]# awk '{sub(/[0-9]/, "", $2); print $0}' b.txt ## 第二行匹配数个数字 0001 0081223efs333kjfdj EREADFASDLKJCV 0002 0081208djfks2288daa JDKFJALSDJFsddf 0003 0081208efskjfdj EREADFASDLKJCV 0004 0081211djfksdaa1234 JDKFJALSDJFsddf [root@localhost test]# awk '{sub(/[0-9]+/, "", $2); print $0}' b.txt ## 匹配第二行首个连续的数字 0001 efs333kjfdj EREADFASDLKJCV 0002 djfks2288daa JDKFJALSDJFsddf 0003 efskjfdj EREADFASDLKJCV 0004 djfksdaa1234 JDKFJALSDJFsddf
。