与awk
:
awk -F, '$2 ~ $1"$"' file.csv
使用grep
,grep
默认情况下仅打印匹配的行:
grep -E '^([^,]+),.*\1$' file.csv
使用sed
,符合条件的打印行:
sed -nE '/^([^,]+),.*\1$/ p' file.csv
范例:
% cat file.txt
site1.com,mail.site1.com
site2.com,testmail.com
site3.com,mx.site3.com
site4.com,smtp.site4.com
site5.com,foomail.com
site6.com,barmail.com
site7.com,webmail.site7.com
site8.com,01mx.site8.com
site9.com,foobarmail.com
site10.com,mx-smtp222.site10.com
% awk -F, '$2 ~ $1"$"' file.txt
site1.com,mail.site1.com
site3.com,mx.site3.com
site4.com,smtp.site4.com
site7.com,webmail.site7.com
site8.com,01mx.site8.com
site10.com,mx-smtp222.site10.com
% grep -E '^([^,]+),.*\1$' file.txt
site1.com,mail.site1.com
site3.com,mx.site3.com
site4.com,smtp.site4.com
site7.com,webmail.site7.com
site8.com,01mx.site8.com
site10.com,mx-smtp222.site10.com
% sed -nE '/^([^,]+),.*\1$/ p' file.txt
site1.com,mail.site1.com
site3.com,mx.site3.com
site4.com,smtp.site4.com
site7.com,webmail.site7.com
site8.com,01mx.site8.com
site10.com,mx-smtp222.site10.com