Sed - Replacing numbers w/asteriks in random location in a file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Sed - Replacing numbers w/asteriks in random location in a file
# 1  
Old 10-02-2001
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  
Old 10-02-2001
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  
Old 10-02-2001
Oh yeah, you might wanna put a "g" on the end of that sed script to get all of 'em...

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



- dEvNuL
# 4  
Old 10-02-2001
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  
Old 10-03-2001
Error

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  
Old 10-03-2001
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  
Old 10-03-2001
*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
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Random numbers

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! Write a shell script that will take the sum of two random number? Ex: Random n1 +Random n2 = result i tries to write it but i had some dufficulties ... (3 Replies)
Discussion started by: renegade755
3 Replies

2. Shell Programming and Scripting

A way to store 2 random numbers from a for loop?

I have a for loop that cycles twice and generates 1 random number for each pass through. I would like to be able to store the two numbers to use later for arithmetics. Is there a way to do that? Right now I can only seem to use the last random number for anything. Thanks. (4 Replies)
Discussion started by: AxlVanDamme
4 Replies

3. Shell Programming and Scripting

Random float numbers in BASH

Hi people :) I'm learning shell scripting using bash and I want to generate 4 floating point number with 5 decimal places and write them to a file and a variable. I've done all this except the $RAMDOM enviroment variable does not generate a float number but a integrer. I hope you could... (3 Replies)
Discussion started by: pharaoh
3 Replies

4. Shell Programming and Scripting

Replace a random string of numbers

Hi Can someone help me with this one? I have string.. (PROC_PROC_ID == 12183) <--PID is dynamic and i want to replace the PID number with whatever PID from /opt/hpws/apache32_2/logs/httpd.pid file. i'm having problem since the PID on the string is dynamic. It may be 2-5 digits or more. ... (5 Replies)
Discussion started by: ryandegreat25
5 Replies

5. Shell Programming and Scripting

Generating random numbers

Hi, I am having trouble with generating random numbers. can this be done with awk? So I have a file that looks like this: 23 30 24 40 26 34 So column1 is start and column2 is end. I want to generate 3 random #'s between start and stop: So the output will look like this: ... (9 Replies)
Discussion started by: phil_heath
9 Replies

6. Shell Programming and Scripting

Random Numbers - Perl

Hi Guys I have a script to find Ranomd numbers. But I want to make the file to produce more random. Could u guys help me plz. In this Script I have the code that generates random in for loop and the range I have specified in my %chromlength input and out put will be like this chrno start end... (3 Replies)
Discussion started by: repinementer
3 Replies

7. UNIX for Dummies Questions & Answers

Retrieving random numbers out of a text file

Hi one and all, I'm working on a Bash script that is designed to calculate how much IP traffic has passed through a port to determine traffic volume over a given amount of time. I've currently been able to use the netstat -s command coupled with grep to write to a file the total packets... (13 Replies)
Discussion started by: nistleloy
13 Replies

8. Shell Programming and Scripting

Random numbers from 0 to 1000

Hello All, I want to make a simple script which generate random number from 0 to 1000. and simply display it. Plz HELP!!!!!! Regards, Waqas Ahmed (2 Replies)
Discussion started by: wakhan
2 Replies

9. UNIX for Dummies Questions & Answers

Random numbers without repetition

Is anyone know some scripts to generate random number without repetition using bash; for example generate 10 different random numbers. Thanks (8 Replies)
Discussion started by: asal_email
8 Replies
Login or Register to Ask a Question