ZhangZhihui's Blog  
awk '/regexp/{for(i=1;i<=NF;++i)if($i~/regexp/)print $i}' /path/to/inputfile

scnzzh@ZUBT:~$ echo 'a=1 b=2' | awk '/a=/{for(i=1;i<=NF;++i)if($i~/a=/)print $i}'
a=1

 

scnzzh@ZUBT:~$ echo "aaa[01:100].com bbb" | awk '/[*:*]/{for(i=1;i<=NF;i++)if($i~/[*:*]/)print $i}'
aaa[01:100].com

 

NF is a predefined variable whose value is the number of fields in the current record. awk automatically updates the value of NF each time it reads a record.

ZUBT:~ $>ls -ltr| tail -1
drwxrwxr-x 3 zzh zzh 4096 Oct 24 18:03 aaa

ZUBT:~ $>ls -ltr| tail -1| awk '{print $NF}'
aaa

 

posted on 2020-11-13 13:44  ZhangZhihuiAAA  阅读(67)  评论(0编辑  收藏  举报