Masking algorithm


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Masking algorithm
# 1  
Old 07-31-2015
Masking algorithm

I have a requirement of masking few specific fields in the UNIX file. The details are as following-

File is fixed length file with each record of 250 charater length.

2 fields needs to be masked – the positions are 21:30 and 110:120

The character by character making needs to be done which means one character in this field (21:30 and 110:120) should be replaced with some another character.

The replacement patterns should be kept in a separate file. For example
Code:
A:B
B:C

Which means A should be replaced with B , B with C and so on.

Last edited by Don Cragun; 07-31-2015 at 10:09 PM.. Reason: Add CODE and ICODE tags.
# 2  
Old 07-31-2015
Is this a homework assignment. Homework assignments must be filed in the Homework & Coursework forum and the 1st post in threads in that forum must contain a completely filled out homework template.

If it isn't homework, you need to make your requirements much clearer. A masking operation would blank out or remove a specific character or a specific set of characters; not replace one set of characters with another set of characters.

And without sample input and corresponding sample output (in CODE tags), your specification is ambiguous. If an A is found and converted to a B (as in your sample), is the output for that position supposed to be aB or should it be a C because the 2nd line in your sample input file says B should be changed to C?
# 3  
Old 08-02-2015
This is not an assignment; this is a real time challenge. Apologies for not articulating problem in the correct manner and fully explaining the issue.
Maybe calling it a masking solution is not the right thing. My actual requirement is to replace a set of characters with another set of character. That too a character by character replacement so reverse decryption can be easily achieved as well.
As it’s character by character replacement every character will have one and only one replacement character. Hence ABCDE should convert to BCDEF where replacement algorithm is like
Code:
A : B
B : C
C : D
D : E
E : F

The first character to be found and replaced by second character.
# 4  
Old 08-02-2015
Given your separate translation file has the structure as in post#1 (no spaces), this might do what you need:
Code:
awk '
FNR==NR         {C[$1]=$2
                 next
                }

FNR==1          {MXR=split (RANGES, T1, " ")
                 for (i=1; i<=MXR; i++) {split (T1[i], T2, ":")
                                         S[i]=T2[1]
                                         E[i]=T2[2]
                                        }
                }

                {for (i=1; i<=MXR; i++)
                        {for (j=S[i]; j<=E[i]; j++)
                                if (C[$j]) $j=C[$j]
                        }
                }
1
' FS=":" file1 FS="" OFS="" RANGES="21:30 110:120" file2

# 5  
Old 08-02-2015
If your replacement pattern file is in the format shown in post #1 or in the format shown in post #3, or if you are working on a system where setting FS to the empty string doesn't treat each input character as a separate field, you could this (although all of the field sizes and offsets are built into the code instead of being derived from an input operand):
Code:
awk -F'[[:blank:]]*:[[:blank:]]*' '
FNR == NR {
	rp[$1] = $2
	next
}
{	o = substr($0, 1, 20)
	for(i = 21; i <= 30; i++)
		o = o (((c = substr($0, i, 1)) in rp) ? rp[c] : c)
	o = o substr($0, 31, 79)
	for(i = 110; i <= 120; i++)
		o = o (((c = substr($0, i, 1)) in rp) ? rp[c] : c)
	print o substr($0, 121)
}' replacement_pattern_file data_file

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk.
# 6  
Old 08-02-2015
Code snippets working

Thanks RudiC and Don.

The codes are working beautifully and are doing the intended translations.

My next challenge is the performance as these codes many need to run on files with 10M records. However I had a test run with 1M records and it completed in less than 8 minutes.

I will further post if I get any further challenge in this. Need to build a complete solution across files ( with multiple translation fields).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Masking with gsub command

My file "test.dat" data as below Requirement is to mask(replace) all english characters with "X" EXCEPT first 7 characters of every line. my command awk '{gsub("]","X")}1' test.dat looks not working properly, Appreciate any suggestion... (6 Replies)
Discussion started by: JSKOBS
6 Replies

2. 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

3. 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

4. UNIX for Advanced & Expert Users

.htaccess redirect with masking

I am looking to forward the following with masking via my .htaccess file: www.mywebsite.com/origpage www.mywebsite.com/newpage I do not want to forward the entire site, just this one page with masking. Neither page has an extension. I am able to forward with .htaccess - but it does not mask the... (1 Reply)
Discussion started by: globalnerds
1 Replies

5. Shell Programming and Scripting

masking issue

Hi I am facing an issue with the below script which has the below line each field being separated with a tab. I need to mask the 8 and 7th field based on following conditions 1. 8th field is 16 in length and is numerics i will mask the middle 6 digits except the first 6 and last 4. input... (2 Replies)
Discussion started by: mad_man12
2 Replies

6. Emergency UNIX and Linux Support

Masking of number

BAT:0310:2009-08-0:Y4 :H:D:00003721:03103721.IFH:00138770:05767:00000000001279' EXR:CLP:912.570000' STA:A:9071559:2009-08-10::Wer::Mrs' DEF::531.97:531.97:310221661617::+ABC:BAL:1:N::::5:40.00:0.00:2009-08-10:CN:1111111111109962::3:N:missc :N:PH:00010833:... (5 Replies)
Discussion started by: mad_man12
5 Replies

7. UNIX for Dummies Questions & Answers

Redirecting site using .htaccess with masking

I am trying to redirect my site to another with masking using .htaccess. I have the redirect working, but it does not mask. Here is what I have in my .htaccess: RewriteEngine On rewritecond %{http_host} ^http://www.securepaynet.net/default.aspx?ci=1767&prog_id=globalnerds RewriteRule /... (0 Replies)
Discussion started by: globalnerds
0 Replies

8. 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

9. Shell Programming and Scripting

Masking Content of a String

Hello, I need to know that whether a content of a string can be hidden or masked inside a shell script. My Sample Code is given below <Code> #!/usr/bin/ksh Userid=test DB=temp Passwd=`java Decryption test` # The Above command will get the encryped password for "test" user id and store... (2 Replies)
Discussion started by: maxmave
2 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