Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Matching numbers to mask it

I have a text file like below. It’s a position-based text file. The positions from 13 to 30 represent bank account numbers and need to be masked with an asterisk if a number exists. What’s the best way to do this? My OS is Oracle Solaris 11.4

6220750000610032002137       0000404584               MCKENNES JAITLIN C      0071000280000122
622075000079643811983        0000420000               RICHOTTA AMENDA         0071000240000134
632275079784326217002        0000340000               KOHLDECK CARA           0071000230000146
622101205691399004002904006210000610000               MEIER NICHOLAS          0071000270000222

Expected Outcome

622075000061**********       0000404584               MCKENNES JAITLIN C      0071000280000122
622075000079*********        0000420000               RICHOTTA AMENDA         0071000240000134
632275079784*********        0000340000               KOHLDECK CARA           0071000230000146
622101205691*****************0000610000               MEIER NICHOLAS          0071000270000222

I am trying the below command but getting an error.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

gawk '{if (match($0, "[0-9]", 13, 27)) $0 = substr($0, 1, 12) "*" substr($0, 27)}' PAY.EM

gawk: cmd. line:1: {if (match($0, "[0-9]", 13, 27)) $0 = substr($0, 1, 12) "*" substr($0, 27)}
gawk: cmd. line:1:  

                         ^ 4 is invalid as number of arguments for match

>Solution :

As @jared_mamrot comments, it may be difficult to define the ‘best’. Here is a posix-compliant solution just for information:

awk '{s = substr($0, 13, 17); gsub(/[0-9]/, "*", s); print(substr($0, 1, 12) s substr($0, 30))}' PAY.EM

Result:

622075000061**********       0000404584               MCKENNES JAITLIN C      0071000280000122
622075000079*********        0000420000               RICHOTTA AMENDA         0071000240000134
632275079784*********        0000340000               KOHLDECK CARA           0071000230000146
622101205691*****************0000610000               MEIER NICHOLAS          0071000270000222
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading