全局域在"*@“选项中,当电子邮件与这些全局域之一匹配时,我需要将它们从列表中排除。
示例:
WF,*@stackoverflow.com
WF,*@superuser.com
WF,*@stackexchange.com
WF,test@superuser.com
WF,test@stackapps.com
WF,test@stackexchange.com输出:
WF,*@stackoverflow.com
WF,*@superuser.com
WF,*@stackexchange.com
WF,test@stackapps.com发布于 2012-10-29 18:50:03
$ awk -F, 'NR==FNR && /\*@/{a[substr($2,3)]=1;print;next}NR!=FNR && $2 !~ /^\*/{x=$2;sub(/.*@/,"",x); if (!(x in a))print;}' OFS=, file file
WF,*@stackoverflow.com
WF,*@superuser.com
WF,*@stackexchange.com
WF,test@stackapps.com发布于 2012-10-29 18:59:57
您在同一文件中有两种类型的数据,因此最简单的处理方法是首先对其进行划分:
<infile tee >(grep '\*@' > global) >(grep -v '\*@' > addr) > /dev/null然后使用global从addr中删除信息
grep -vf <(cut -d@ -f2 global) addr把它们放在一起:
<infile tee >(grep '\*@' > global) >(grep -v '\*@' > addr) > /dev/null
cat global <(grep -vf <(cut -d@ -f2 global) addr) > outfileoutfile的内容
WF,*@stackoverflow.com
WF,*@superuser.com
WF,*@stackexchange.com
WF,test@stackapps.com使用rm global addr清理临时文件。
发布于 2012-10-29 18:40:35
你可以这样做:
grep -o "\*@.*" file.txt | sed -e 's/^/[^*]/' > global.txt
grep -vf global.txt file.txt这将从提取全局电子邮件开始,并在它们前面加上[^*],将结果保存到global.txt中。然后将该文件用作grep的输入,在grep中,每一行都被视为[^*]*@global.domain.com形式的正则表达式。-v选项告诉grep只打印与该模式不匹配的行。
另一个类似的选择是,使用sed进行就地编辑:
grep -o "\*@.*" file.txt | sed -e 's/^.*$/\/[^*]&\/d/' > global.sed
sed -i -f global.sed file.txthttps://stackoverflow.com/questions/13119804
复制相似问题