Computing.Net > Forums > Unix > Find and replace with sed or awk

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Find and replace with sed or awk

Reply to Message Icon

Name: pnbalaji
Date: April 3, 2008 at 14:09:35 Pacific
OS: AIX 5.3
CPU/Ram: 8GB
Product: 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.

Can some one help me?

Thanks,
Balaji.



Sponsored Link
Ads by Google

Response Number 1
Name: ghostdog
Date: April 3, 2008 at 19:21:47 Pacific
Reply:


awk '/<doc_no>/{
gsub(/<doc_no>|<\/doc_no>/,"");
l=length($0)
print "<doc_no>" substr($0,1,6)" "substr($0,l-4) "</doc_no>"
next
}1' file



0

Response Number 2
Name: pnbalaji
Date: April 7, 2008 at 13:16:28 Pacific
Reply:

Hi,

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.

Can you please help?

Thanks,
Balaji.


0

Response Number 3
Name: pnbalaji
Date: April 7, 2008 at 13:17:32 Pacific
Reply:

Just to clarify, it loses the formatting of the credit card line alone, not the entire xml.

Thanks,
Balaji.


0

Response Number 4
Name: ghostdog
Date: April 7, 2008 at 18:34:33 Pacific
Reply:

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.


0

Response Number 5
Name: pnbalaji
Date: April 8, 2008 at 06:31:52 Pacific
Reply:

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.

Hope this helps.

Thanks,
Balaji.


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Find and replace with sed or awk

Search And Replace With Sed www.computing.net/answers/unix/search-and-replace-with-sed/3845.html

find and replace stings www.computing.net/answers/unix/find-and-replace-stings/3865.html

One more Q. (find and replace) www.computing.net/answers/unix/one-more-q-find-and-replace/5826.html