Name: pnbalaji Date: April 3, 2008 at 14:09:35 Pacific Subject: Find and replace with sed or awk OS: AIX 5.3 CPU/Ram: 8GB Model/Manufacturer: IBM, 7026-H80
Comment:
Hi,
I have a problem in my requirement. I have a file called 1000.log which is basically an xml for an internet order at our site. The xml will contain a tag as given below.
<doc_no>5454545454545454</doc_no>
This doc_no field contains the unmasked credit card #. My requirement is to replace the characters between the first 6 and last 4 with spaces. After change, this line should be like as shown below.
<web_ordno>545454 5454</web_ordno>
The first 6 and the last 4 characters should be retained and the characters in between them should be replaced with spaces. Also, this should work for irrespective of the type of credit card.
For ex, visa card can be of 13 or 16 characters in length, AMEX card will be of 15 chars in length, Diner's club card will be of 14 chars in length.
Thanks for your reply. However, I am having some problem with the code.
My code is given below.
============================================= for stagedFile in $stagingDir/*.orders do echo "Processing $stagedFile.." awk '/<\/doc_no>/ { gsub(/<doc_no>|<\/doc_no>/,""); print $0 l=length($0) if ( l > 8 ) { print "<doc_no>"substr($0,1,6)" "substr($0,l-4)"</doc_no>" } next }1' $stagedFile done =============================================
Please see the sample output below. ============================================= <pay_documents>
<doc_type>CC</doc_type>
Credit card # is 4060420233588945 and its length is 27 <doc_no> 8945</doc_no> <doc_date>1005</doc_date>
</pay_documents>
=============================================
I displayed the credit card # just for debugging. There are couple of errors in this which I am listing below.
1. The length is shown as 27 though it is a 16 digit card #. 2. It loses the formatting of the XML. The lines in the xml are intended and after running this awk script, it loses the formatting. 3. It is displaying only the last 4 digits of the credit card where it is supposed to display the first 6 digits and last 4 digits of the card #.
I assume the card length is calculated by adding the leading spaces also. I tried to fix it, but could not.
where is the part where you displayed the message ?? i didn't see it in your code. My guess is that you displayed whole length of the record, not the credit card numbers.
I have this code in a script called ccmask.sh and I am executing as below.
ccmask.sh /met/le/ocs | tee test.log
I am getting the stagingDir as a command line parameter. The output that I pasted is from test.log. When I run the script, it displays the contents of all XMLS on the screen, which I am rewriting into a file called test.log using the tee command. If you can provide your email id, I can forward the original XML and the test.log file to you.
The information on Computing.Net is the opinions of its users. Such
opinions may not be accurate and they are to be used at your own risk.
Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE