awk之提取联系人邮箱(用到for循环、gensub等)

【问题描述】

从以下文件中提取联系人邮箱地址,内容如下:

<[email protected]>, 李俊清 <[email protected]>, 任翔 <[email protected]>, 李杨柳 <[email protected]>, 孟津 <[email protected]>, 王立光 <[email protected]>

【解决办法】

1.awk

$awk '{for(i=1;i<=NF;i++) if($i ~ "<" ) {print gensub(/<(.+)>.*/,"\\1","g",$i)} }' a.txt 
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
$awk '{for(i=1;i<=NF;i++) if($i ~ "<" ) {print $i} }' a.txt |awk -F '<|>' '{print $2}'
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]

2.sed与awk结合

$sed -r 's#,##g' a.txt |tr ">" "\n" |awk -F '<' '{print $NF}' |sed '/^\s*$/d'
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]

sed '/^\s*$/d'删除空行

3.grep

$grep -oP '(?<=<)(.*?)(?=>)' a.txt 
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]


你可能感兴趣的:(awk,三剑客)