用於屏蔽文件中信用卡數字的正規表示式

用於屏蔽文件中信用卡數字的正規表示式

使用 sed 封鎖檔案中除信用卡號最後 4 位元之外的所有數字的最簡單方法是什麼。

使用egrep和以下正規表示式,我們可以找到Visa、Mastercard、Discover、American Express、Diner's Club和JCB的號碼。

egrep "(?:4[0-9]{12}(?:[0-9]{3})?|5[12345][0-9]{14}|3[47][0-9]{13}|3(?:0[012345]|[68][0-9])[0-9]{11}|6(?:011|5[0-9]{2})[0-9]{12}|(?:2131|1800|35[0-9]{3})[0-9]{11})" transactions.log

請記住,此 transactions.log 中可能存在其他重要數字。該號碼不應被觸摸,並採用信用卡號以​​外的其他格式

信用卡號碼格式範例:38012345678901 其他號碼格式範例:2014-02-11 22:23, 1134-53553, 4-5-6-7-7-2

簽證:4123456789012, 4123456789012345,41234567890123456

萬事達卡:5123456789012345, 512345678901234,51234567890123455

發現:6011123456789012651234567890123460111234567890160111234567890123

美國運通: 341234567890123, 371234567890123, 34123456789012, 301234567890123, 3712345678901234

晚餐俱樂部:37012345678901

JCB:213112345678901, 180012345678901, 3512345678901234, 21311234567890, 18001234567890, , 2131123456789012, 1800123456789012, 35123456789012

最簡單的查找和替換是?

答案1

要將前 10 位數字替換為*當且僅當該數字剛好有 14 位數字時:

sed 's/\([^0-9]\)[0-9]\{10\}\([0-9]\{4\}\)\([^0-9]\|$\)/\1**********\2\3/g'

例子:

$ echo 'foo bar 38012345678901 2014-02-11 22:23, 1134-53553, 4-5-6-7-7-2, 
  28012345678901,,,,, 3801234567890123456789 stuff' | \
  sed 's/\([^0-9]\)[0-9]\{10\}\([0-9]\{4\}\)\([^0-9]\|$\)/\1**********\2\3/g'

foo bar **********8901 2014-02-11 22:23, 1134-53553, 4-5-6-7-7-2,
  **********8901,,,,, 3801234567890123456789 stuff

編輯

若要完全符合更新範例中的模式:

sed 's/\([^0-9]\)4[0-9]\{8\}\([0-9]\{4\}\)\([^0-9]\|$\)/\1**********\2\3/g;s/\([^0-9]\)4[0-9]\{11\}\([0-9]\{4\}\)\([^0-9]\|$\)/\1**********\2\3/g;s/\([^0-9]\)4[0-9]\{12\}\([0-9]\{4\}\)\([^0-9]\|$\)/\1**********\2\3/g;s/\([^0-9]\)5[12345][0-9]\{10\}\([0-9]\{4\}\)\([^0-9]\|$\)/\1**********\2\3/g;s/\([^0-9]\)5[12345][0-9]\{9\}\([0-9]\{4\}\)\([^0-9]\|$\)/\1**********\2\3/g;s/\([^0-9]\)5[12345][0-9]\{11\}\([0-9]\{4\}\)\([^0-9]\|$\)/\1**********\2\3/g;s/\([^0-9]\)3[47][0-9]\{9\}\([0-9]\{4\}\)\([^0-9]\|$\)/\1**********\2\3/g;s/\([^0-9]\)30[12345][0-9]\{8\}\([0-9]\{4\}\)\([^0-9]\|$\)/\1**********\2\3/g;s/\([^0-9]\)3[47][0-9]\{10\}\([0-9]\{4\}\)\([^0-9]\|$\)/\1**********\2\3/g;s/\([^0-9]\)3[47][0-9]\{8\}\([0-9]\{4\}\)\([^0-9]\|$\)/\1**********\2\3/g;s/\([^0-9]\)6011[0-9]\{8\}\([0-9]\{4\}\)\([^0-9]\|$\)/\1**********\2\3/g;s/\([^0-9]\)6011[0-9]\{9\}\([0-9]\{4\}\)\([^0-9]\|$\)/\1**********\2\3/g;s/\([^0-9]\)6011[0-9]\{7\}\([0-9]\{4\}\)\([^0-9]\|$\)/\1**********\2\3/g;s/\([^0-9]\)65[0-9]\{10\}\([0-9]\{4\}\)\([^0-9]\|$\)/\1**********\2\3/g;s/\([^0-9]\)2131[0-9]\{6,8\}\([0-9]\{4\}\)\([^0-9]\|$\)/\1**********\2\3/g;s/\([^0-9]\)1800[0-9]\{6,8\}\([0-9]\{4\}\)\([^0-9]\|$\)/\1**********\2\3/g;s/\([^0-9]\)35[0-9]\{8,10\}\([0-9]\{4\}\)\([^0-9]\|$\)/\1**********\2\3/g'

這適用於以下文件:

Example of credit card number format: 38012345678901 

Visa: 4123456789012, 4123456789012345, 41234567890123456

MasterCard: 5123456789012345, 512345678901234, 51234567890123455

Discover: 6011123456789012, 6512345678901234, 601112345678901, 60111234567890123

American Express: 341234567890123, 371234567890123, 34123456789012, 301234567890123, 3712345678901234

Diner's Club: 37012345678901

JCB: 213112345678901, 180012345678901, 3512345678901234, 21311234567890, 18001234567890, ,2131123456789012, 1800123456789012, 35123456789012

Example of other numbers format: 2014-02-11 22:23, 1134-53553, 4-5-6-7-7-2

給出

Example of credit card number format: 38012345678901 

Visa: **********9012, **********2345, **********3456

MasterCard: **********2345, **********1234, **********3455

Discover: **********9012, **********1234, **********8901, **********0123

American Express: **********0123, **********0123, **********9012, **********0123, **********1234

Diner's Club: **********8901

JCB: **********8901, **********8901, **********1234, **********7890, **********7890, ,**********9012, **********9012, **********9012

Example of other numbers format: 2014-02-11 22:23, 1134-53553, 4-5-6-7-7-2

當然,首字母38012345678901不匹配,因為它不存在於任何卡片圖案列表中。

相關內容