Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Sed - Replacing numbers w/asteriks in random location in a file Post 7887 by devnul on Wednesday 3rd of October 2001 12:26:55 AM
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
 

9 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

9. 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
CSPLIT(1)						    BSD General Commands Manual 						 CSPLIT(1)

NAME
csplit -- split files based on context SYNOPSIS
csplit [-ks] [-f prefix] [-n number] file args ... DESCRIPTION
The csplit utility splits file into pieces using the patterns args. If file is a dash ('-'), csplit reads from standard input. Files are created with a prefix of ``xx'' and two decimal digits. The size of each file is written to standard output as it is created. If an error occurs whilst files are being created, or a HUP, INT, or TERM signal is received, all files previously written are removed. The options are as follows: -f prefix Create file names beginning with prefix, instead of ``xx''. -k Do not remove previously created files if an error occurs or a HUP, INT, or TERM signal is received. -n number Create file names beginning with number of decimal digits after the prefix, instead of 2. -s Do not write the size of each output file to standard output as it is created. The args operands may be a combination of the following patterns: /regexp/[[+|-]offset] Create a file containing the input from the current line to (but not including) the next line matching the given basic regular expression. An optional offset from the line that matched may be specified. %regexp%[[+|-]offset] Same as above but a file is not created for the output. line_no Create containing the input from the current line to (but not including) the specified line number. {num} Repeat the previous pattern the specified number of times. If it follows a line number pattern, a new file will be created for each line_no lines, num times. The first line of the file is line number 1 for historic reasons. After all the patterns have been processed, the remaining input data (if there is any) will be written to a new file. Requesting to split at a line before the current line number or past the end of the file will result in an error. ENVIRONMENT
The LANG, LC_ALL, LC_COLLATE and LC_CTYPE environment variables affect the execution of csplit as described in environ(7). EXIT STATUS
The csplit utility exits 0 on success, and >0 if an error occurs. EXAMPLES
Split the mdoc(7) file foo.1 into one file for each section (up to 21 plus one for the rest, if any): csplit -k foo.1 '%^.Sh%' '/^.Sh/' '{20}' Split standard input after the first 99 lines and every 100 lines thereafter: csplit -k - 100 '{19}' SEE ALSO
sed(1), split(1), re_format(7) STANDARDS
The csplit utility conforms to IEEE Std 1003.1-2001 (``POSIX.1''). HISTORY
A csplit command appeared in PWB UNIX. BUGS
Input lines are limited to LINE_MAX (2048) bytes in length. BSD
February 6, 2014 BSD
All times are GMT -4. The time now is 10:40 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy