查找信用卡号并在设定位置替换字符

时间:2016-04-17 06:10:04

标签: replace sed position

我有一个包含信用卡号码(16个字符)的文件,我想找到它们并用" X"除了前6个和后4个数字。

sed -i 's/\([345]\{1\}[0-9]\{3\}\|6011\)\{1\}[ -]\?[0-9]\{4\}[ -]\?[0-9]\{2\}[-]\?[0-9]\{2\}[ -]\?[0-9]\{1,4\}/\XXXXXX/g' foobar.csv

将轻松找到文件中包含的所有信用卡,并将其替换为" XXXX"

但是我想找到信用卡并用#34; X"替换字符串中的字符串7-12,因此该文件将包含像123456XXXXXX7890这样被屏蔽的信用。

示例输入行:

jam peanut boat handbag on the shore in Tuesday 4548640040302006 in the morning jimmy

示例输出行:

jam peanut boat handbag on the shore in Tuesday 454864XXXXXX2006 in the morning jimmy

1 个答案:

答案 0 :(得分:0)

尝试使用GNU sed来匿名化信用卡号码:

sed -i 's/\(\([345]\{1\}[0-9]\{3\}\|6011\)\{1\}[ -]\?[0-9]\{2\}\)[0-9]\{2\}[ -]\?[0-9]\{2\}[-]\?[0-9]\{2\}[ -]\?\([0-9]\{1,4\}\)/\1XXXXXX\3/g' file

输入:

jam peanut boat handbag on the shore in Tuesday 4548640040302006 in the morning jimmy
jam peanut boat handbag on the shore in Tuesday 454864XXXXXX2006 in the morning jimmy

输出到文件:

jam peanut boat handbag on the shore in Tuesday 454864XXXXXX2006 in the morning jimmy
jam peanut boat handbag on the shore in Tuesday 454864XXXXXX2006 in the morning jimmy