awk help to mask characters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk help to mask characters
# 1  
Old 11-02-2018
awk help to mask characters

Hi Guys, I'm not an expert so seeking advice on awk.
I need to mask numbers in a text field so that they show up as "*" except for the last 4.
Input:
Quote:
His phone number is (123) 456-7890 and account is 34567 887272.
Desired Output:
Quote:
His phone number is (***) ***-7890 and account is ***** **7272.
The part of the Code I am using gives me this Output
Quote:
His phone number is **********7890 and account is ********7272.

Code:
num=$(echo $num | awk 'BEGIN{FS=OFS=""}{for (i=1 ; i<=NF-4; i++) $i="*"} 1')

Here num is only the number that I am passing "(123) 456-7890 " or "34567 887272"

Thanks in advance for the help.
# 2  
Old 11-02-2018
this is a bit convoluted - others might find a better, more generic approach - this is brute-force:
Code:
$ echo 'His phone number is (123) 456-7890 and account is 34567 887272. ' | \
   awk '
     BEGIN{m="************"} 
     {
      l=length($5);
      $5="(" substr(m,2,l-2) ")"

      l=index($6,"-")
      $6=substr(m,1,l-1) substr($6,l)

     l=length($(NF-1))
     $(NF-1)=substr(m,1,l)

     match($NF,".*[^0-9]")
     $NF=substr(m,1,RLENGTH-5) substr($NF,RLENGTH-4)
}
1'

produces:
Code:
His phone number is (***) ***-7890 and account is ***** **7272.


Last edited by vgersh99; 11-02-2018 at 05:59 PM..
This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 11-02-2018
My idea would be to condition the assignment of an asterisk to $i only when it is a digit, i.e.:
Code:
if($i~="[0-9]")$i="*"

Hope it helps
# 4  
Old 11-02-2018
Quote:
Originally Posted by hexram
My idea would be to condition the assignment of an asterisk to $i only when it is a digit, i.e.:
Code:
if($i~="[0-9]")$i="*"

Hope it helps
It's slightly more complicated than that - look at the sample input/output examples.

Last edited by vgersh99; 11-02-2018 at 05:59 PM..
This User Gave Thanks to vgersh99 For This Post:
# 5  
Old 11-03-2018
This might work:
Code:
echo 'His phone number is (123) 456-7890 and account is 34567 887272. ' | awk '
        {while (match ($0, /[0-9()-]+ *[0-9()-]*/))     {X = substr ($0, RSTART, RLENGTH-4)
                                                         gsub (/[0-9]/, "*", X)
                                                         printf "%s%s%s", substr ($0, 1, RSTART-1), X, substr ($0, RSTART + RLENGTH - 4, 4)
                                                         $0 = substr ($0, RSTART + RLENGTH)
                                                        }
         print
        }
'
His phone number is (***) ***-7890 and account is ***** **7272.

The regex needs to be quite cumbersome as my mawk doesn't allow for back references. It will fail, too, if there are non-digits in the last four places.
These 2 Users Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk - checking last three characters

Hello, I am working with some very large files (upwards of 1M records). I have written code to parse out a lot of the data and am using awk rather than a built-in "while read LINE" for performance (I have tested both ways). That said, I now need to read each of these incoming lines, check the ninth... (2 Replies)
Discussion started by: dagamier
2 Replies

2. UNIX for Dummies Questions & Answers

What is mask and effective right mask in setfacl?

Hi Guys, can someone explain what is mask and effective right mask in setfacl and getfacl command with example, unable to get it. (3 Replies)
Discussion started by: Jcpratap
3 Replies

3. Red Hat

Samba create mask and dir mask on RHEL 4.8

Hi Experts, I'm using samba -3.6.1 on Red Hat Enterprise Linux ES release 4 (Nahant Update 8) ,all seems ok. The issue im facing is as follows. When ever a user creates a file via windows explorer the permissions assgined to the file are as follows -rw-rwxr--+ 1 tom group2 0 Mar 9... (1 Reply)
Discussion started by: maverick_here
1 Replies

4. UNIX for Dummies Questions & Answers

how to mask the password ?

Hi All, i am executing peoplesoft sqr command from unix prompt which has the unix id/password as parameter along with other parameters. i want to show whole command in log file but want to mask the id/password field. this command i am executing in shell script. Please suggest.. >sqr sqrname... (2 Replies)
Discussion started by: avi.coep
2 Replies

5. Shell Programming and Scripting

Use awk to print first 6 characters

Hi, i want to use awk to print the first 6 characters of a variable awk -F"|" '$3>0 { print $3 }' z00.unl > z001.unl but $3= 7 digits and i just want to print the first 6 digits. eg 1005779 but i want to print only 100577 (3 Replies)
Discussion started by: dealerso
3 Replies

6. Shell Programming and Scripting

Need to Mask Data

I have an requirement. There is a file which has the below contents Unix|123|17-01-2010 .... .... .... .... and so an now each letter has a corresponding predefined mapping letter in order to mask the original data.(for example U = A, n=b, i=c, x=d, same like number 1=9,2=8,3=7. Also... (8 Replies)
Discussion started by: ksmbabu
8 Replies

7. Shell Programming and Scripting

mask ID number

HP-UX 11i v2. #!/bin/sh Hi all. I have a flat file like this with lines like this: |07/19/07|08:26AM|1|CupsCoffee|CupsCo|989898989 |Doe, John Y |THE PLUS CARD - Price| | |Y| | 2.00| I would like to replace the id number (field 6) with a masked... (3 Replies)
Discussion started by: lyoncc
3 Replies

8. IP Networking

Changing the mask

What is the command to change the mask to 255.255.255.0 The system was set up incorectly and the mask needs to be corrected (1 Reply)
Discussion started by: kkinnon
1 Replies

9. Programming

password mask in C

Hi, Could any one help me to write a C program for password mask with ******. I mean whatever word i will type that will show on the screen as ***** and should store the correct value in a variable. Thanks in advance Krishna (5 Replies)
Discussion started by: krishna
5 Replies

10. IP Networking

Subnet mask

Hi, I have about 30 computers for users with subnet mask x.x.x.0, and 25 computers for workers with s.m. x.x.x.128. My server has a s.m. x.x.x.128 so with workers computers I can see my server and all the computers in that s.m., but I can't see the server from the users computers and I need to see... (7 Replies)
Discussion started by: Z0DiaC
7 Replies
Login or Register to Ask a Question