The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to set constrain on random numbers in c ahjiefreak High Level Programming 6 01-11-2008 04:46 AM
how can i isolate the random sequence of numbers using awk? rcon1 UNIX for Dummies Questions & Answers 2 01-09-2008 09:06 AM
replacing text in specific location zeontman HP-UX 1 07-19-2007 05:46 AM
genrating pseudo random numbers hack_tom Shell Programming and Scripting 7 04-27-2007 10:10 PM
Random numbers without repetition asal_email UNIX for Dummies Questions & Answers 8 07-14-2005 04:02 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-02-2001
giannicello giannicello is offline
Registered User
  
 

Join Date: Sep 2001
Location: Phoenix
Posts: 169
Sed - Replacing numbers w/asteriks in random location in a file

I have a file which contains lots of text (comment field). I would like to parse through the comment field which can be up to 255 characters long and look for anything that seems to resemble, say, a credit card number or customer account number, etc. and replace the numbers with asteriks (*).

How do I do this in sed, especially if the numbers can appear anywhere in the comment field? I can do it if the numbers always appear in the same positions but this is not the case. Our users can't seem to follow directions.

I searched the faq and looked at http://wwwinfo.cern.ch/dis/texi2html...2/sed_toc.html but it doesn't seem to have what I am looking for...

Is there a quick way with sed/awk that anyone knows of?? I prefer to do it with sed if possible.

Thank you in advance.
  #2 (permalink)  
Old 10-02-2001
devnul devnul is offline
Registered User
  
 

Join Date: Oct 2001
Location: San Francisco
Posts: 27
Well, lets see a credit card is a 16-digit number in groups of 4 seperated by "-" or a " " character..

This is not the smallest way to do it, but I think it'd do the job:

sed 's/[0-9]\{4\}[- ][0-9]\{4\}[- ][0-9]\{4\}[- ][0-9]\{4\}/\*\*\*\*-\*\*\*\*-\*\*\*\*-\*\*\*\*/'

- dEvNuL
  #3 (permalink)  
Old 10-02-2001
devnul devnul is offline
Registered User
  
 

Join Date: Oct 2001
Location: San Francisco
Posts: 27
Oh yeah, you might wanna put a "g" on the end of that sed script to get all of 'em...

.....\*\*\*\*/g'



- dEvNuL
  #4 (permalink)  
Old 10-02-2001
giannicello giannicello is offline
Registered User
  
 

Join Date: Sep 2001
Location: Phoenix
Posts: 169
Cool! This would work for 16 digit credit cards with dashes; but what if users don't put in dashes or if the 'card' number is 13, or 19 digits (vendor id number, ledger number, etc).
Since I am new to sed, the question would be, if I don't have dashes and I need to check these cases, would this be correct to check, for instance, a 16 digit number?

sed 's/[0-9]\{16\}/\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/'

Thanx. As always, I am learning so much from this website!

Giannicello
  #5 (permalink)  
Old 10-03-2001
devnul devnul is offline
Registered User
  
 

Join Date: Oct 2001
Location: San Francisco
Posts: 27
Exclamation

Well, if you really want to know how to do this right (with sed) *thinks*... There's always a right way and a wrong way to do everything... Of course, the right way is always more complex.... ;-(

I guess I would create a file "filename.sed", with something like the following in it:

<PRE>
s/4[0-9]\{3\}[- ]*[0-9]\{4\}[- ]*[0-9]\{4\}[- ]*[0-9]\{4\}[- ]*[0-9]\{3\}/\*\*\*\*-\*\*\*\*-\*\*\*\*-\*\*\*\*-\*\*\*/g
s/5[1-5][0-9]\{2\}[- ]*[0-9]\{4\}[- ]*[0-9]\{4\}[- ]*[0-9]\{4\}[- ]*[0-9]\{3\}/\*\*\*\*-\*\*\*\*-\*\*\*\*-\*\*\*\*-\*\*
\*/g
s/6011[- ]*[0-9]\{4\}[- ]*[0-9]\{4\}[- ]*[0-9]\{4\}[- ]*[0-9]\{3\}/\*\*\*\*-\*\*\*\*-\*\*\*\*-\*\*\*\*-\*\*\*/g
s/4[0-9]\{3\}[- ]*[0-9]\{4\}[- ]*[0-9]\{4\}[- ]*[0-9]\{4\}/\*\*\*\*-\*\*\*\*-\*\*\*\*-\*\*\*\*/g
s/5[1-5][0-9]\{2\}[- ]*[0-9]\{4\}[- ]*[0-9]\{4\}[- ]*[0-9]\{4\}/\*\*\*\*-\*\*\*\*-\*\*\*\*-\*\*\*\*/g
s/6011[- ]*[0-9]\{4\}[- ]*[0-9]\{4\}[- ]*[0-9]\{4\}/\*\*\*\*-\*\*\*\*-\*\*\*\*-\*\*\*\*/g
s/3[47][0-9]\{13\}/\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/g
</PRE>

To run it I guess you would type sed -f filename.sed < file

It should match:

Line 1: Visa: prefix 4 length 19 digits with optional spaces or dashes between first set of 4 numbers, followed by an optional dash and 3 digits


Line 2: Mastercard: prefix 51 or 55 length 19 digits with optional spaces or dashes between first set of 4 numbers, followed by an optional dash and 3 digits

Line 3: Discover: prefix 6011 length 19 digits with optional spaces or dashes between first set of 4 numbers, followed by an optional dash and 3 digits NOTE: I'm not sure if discover has the optional 3 digits or not if so this line should be removed
Visa: prefix 4 length 19 digits with optional spaces or dashes between first set of 4 numbers, followed by an optional dash and 3 digits

Line 4: Visa: prefix 4 length 16 digits with optional spaces or dashes between first set of 4 numbers.

Line 5: Mastercard: prefix 51 or 55 length 16 digits with optional spaces or dashes between first set of 4 numbers.

Line 6: Discover: prefix 6011 length 16 digits with optional spaces or dashes between first set of 4 numbers

Line 7: American Express: prefix 34 or 37, 15 digits long (no -)


I'm not really sure how well this would work, because I don't have anything to test it against... But, my gut feeling as I put it together is that this is pretty close to what you are looking for?...

- dEvNuL
  #6 (permalink)  
Old 10-03-2001
giannicello giannicello is offline
Registered User
  
 

Join Date: Sep 2001
Location: Phoenix
Posts: 169
DEvNul you're awesome! You seem to know credit cards formats very well so you're the right person to answer. Thank you so much for taking the time to do all that. I would never have come close.

I think that will work for me and I am starting to understand the syntax. I ran the file against the sed script and the numbers in the comment fields were blocked as expected.

I will credit you for the info in my comment for your efforts, unless you object.

Lastly, I tried to do an in-place update against the file 'filename' but this did not work: sed -f ccrules.sed < filname > filename, do I have to pipe from standout to another filename and mv it back to the original 'filename'?

Thanks again!
Giannicello
  #7 (permalink)  
Old 10-03-2001
devnul devnul is offline
Registered User
  
 

Join Date: Oct 2001
Location: San Francisco
Posts: 27
*blush*.. WHy thank you.. :> I certainly would not object...

I wanted to say though that actually I know nothing about credit card numbers, I had to do a search on the internet for credit card formats.....

As for directing to another file and then moving it back, yes that is exactly what you will have to do.......

- dEvNuL
Closed Thread

Bookmarks

Tags
regex, regular expressions

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:47 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0