Masking with gsub command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Masking with gsub command
# 1  
Old 12-08-2016
Masking with gsub command

My file "test.dat" data as below
Quote:
----------------------------
SECN00020161130
SECN10020161130201611301000889CashEWQE FHJIO MKIUO2
SECN105YEA FDSDF WEDCVB 12 JLKJG AA 3/6 ASASSASQ 68000 ZQWEQWQ BHYTREWQ
SECN300
Requirement is to mask(replace) all english characters with "X" EXCEPT first 7 characters of every line.

my command
Code:
awk '{gsub("[[:alpha:]][^SECN]","X")}1' test.dat

looks not working properly, Appreciate any suggestion...
# 2  
Old 12-08-2016
How about
Code:
awk '
        {T = $0
         gsub (/./,"X")
         print substr (T, 1, 7) substr ($0, 8)
        }
' file
-------XXXXXXXXXXXXXXXXXXXXX
SECN000XXXXXXXX
SECN100XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
SECN105XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
SECN300

# 3  
Old 12-08-2016
Thanks for the reply.

Can it be possible to replace only characters, excluding numbers & first 7 characters (output as below...)
Quote:
SECN00020161130
SECN10020161130201611301000889XXXXXXX XXXXX XXXXX2
SECN105XXX XXXXX XXXXX 12 XXXXX XX 3/6 XXXXXXXX 68000 XXXXXXX XXXXXXXX
SECN300
# 4  
Old 12-08-2016
Any idea by yourself?
# 5  
Old 12-08-2016
Code:
awk '{T = $0; gsub("[[:alpha:]]","X"); print substr (T, 1, 7) substr ($0, 8)}' test.dat


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 12-08-2016 at 07:35 AM.. Reason: Added CODE tags.
# 6  
Old 12-08-2016
Quote:
Originally Posted by JSKOBS
Thanks for the reply.
Can it be possible to replace only characters, excluding numbers & first 7 characters (output as below...)
Hello JSKOBS,

Could you please try following and let me know if this helps you.
Code:
 awk '{P=substr($0,1,7);num=split(substr($0,8), A,"");for(i=1;i<=num;i++){if(A[i] !~ /[[:digit:]]/){A[i]="X"};Q=Q?Q A[i]:A[i]};print P Q;Q=""}'   Input_file

Output will be as follows.
Code:
 -------XXXXXXXXXXXXXXXXXXXXX
SECN00020161130
SECN10020161130201611301000889XXXXXXXXXXXXXXXXXXXX2
SECN105XXXXXXXXXXXXXXXXX12XXXXXXXXXX3X6XXXXXXXXXX68000XXXXXXXXXXXXXXXXX

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 7  
Old 12-08-2016
Quote:
Originally Posted by JSKOBS
Code:
awk '{T = $0; gsub("[[:alpha:]]","X"); print substr (T, 1, 7) substr ($0, 8)}' test.dat


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!
Happy with the result?
Looking back at it, it could be slightly simplified:
Code:
awk '{T = substr ($0,1,7) ; gsub("[[:alpha:]]","X"); print T substr ($0, 8)}' file

This User 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 gsub command to replace multiple spaces

Hi Forum. I'm trying to cleanup the following data elements (To remove any occurences of commas and any extra spaces) while preserving the <TAB> delimiter using awk gsub but I have not been successful. Original Data: 4365 monte des source rue,, ,<TAB>trevost<TAB>QC Desired Data:... (1 Reply)
Discussion started by: pchang
1 Replies

2. Shell Programming and Scripting

File creation using awk and gsub command

I have input file 04000912|100:|||||]|101:||]|creDate:1451876825000|1441324800000:]|1444003200000:]|1446595200000:]|1449187200000:]|1451865600000:] I have to get output as below ID|Re_Date|Re_Value|Re_date 04000912|100|40.0|44 04000912|100|50.0|55 04000912|100|60.0|66... (4 Replies)
Discussion started by: shabeena
4 Replies

3. UNIX for Dummies Questions & Answers

Masking data

How Can I mask one particular columns using some unix command? (4 Replies)
Discussion started by: dsa
4 Replies

4. Shell Programming and Scripting

Trouble with passing Variable from bash to awk gsub command

Would really appreciate it if someone could point out my mistake in this line of code, i've been staring blankly at it trying everything i can think of some time now and coming up with nothing. #!/bin/bash echo "Enter Username" read Username awk -F: -v var=${Username} '/^var:/... (9 Replies)
Discussion started by: Nostyx
9 Replies

5. Programming

Masking Password with *'s

So I've been working on this for some time now and can't seem to find the solution that works for me. I'm working in C/Unix. Basically, I want to take a user input and output something different. For example, I want to take a password and output *'s. In another instance, I want to take inputed... (35 Replies)
Discussion started by: bigdrock44
35 Replies

6. Shell Programming and Scripting

Data Masking

I have a pipe delimited file that I need to 'mask' to before loading to keep some data confidential. I need to maintain the first 4 bytes of certain columns and replace the remaining bytes with an 'x'. I would like to maintain spaces but it's not a requirement. Example, need to mask columns 2... (2 Replies)
Discussion started by: 1superdork
2 Replies

7. Shell Programming and Scripting

Masking a column using awk command

Thanks...It was a spoiler for my Team... (5 Replies)
Discussion started by: Diggi
5 Replies

8. Shell Programming and Scripting

Use sed or gsub command

Hi there, I'm trying to write a bash script and I have some difficulties... I have multiple files, which have the following names: file_1.txt file_2.txt ... file_26.txt Within each file there is some information, like: (in the file_1.txt) name of the file: file_name_1_info.hdr (in... (4 Replies)
Discussion started by: giorgos193
4 Replies

9. Shell Programming and Scripting

reg Mailx command or masking of mailid

I am using the mailx command of Unix through which I am able send mails but the problem is the from mail ID is automatically buit by unix as the (UNIX Box Name:---------------------------------) to mask the from id the option used by mailx is -r but for some security reasons this is blocked by the... (1 Reply)
Discussion started by: naren_chella
1 Replies

10. IP Networking

IP Masking

Is it possible for a internal LAN to mask a IP e.g. i have a server ip running the intranet ip being 192.168.0.8 and i want to make that like www.intranet.com is this possible on a internal network ? (1 Reply)
Discussion started by: perleo
1 Replies
Login or Register to Ask a Question