Replace variable length numeric string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace variable length numeric string
# 1  
Old 02-06-2009
Replace variable length numeric string

I have a customer who logged some cc and bank account numbers in their apache logs. I got the cc numbers x'd out with
Code:
sed -e 's/args=[0-9]\{16\}/args=XXXXXXXXXXXXXXXX/g' -e 's/cardnum=[0-9]\{16\}/cardnum=XXXXXXXXXXXXXXXX/g'

but that wasn't too difficult due to the value being 16 digits.

The bank account info is different, might be 6 digits, might be 10, might be 15. The actual string in the log entry will contain
Code:
args=123456789&acctnum=123456789

somewhere in the middle of the line.

Now that I think about it, the new value doesn't have to match the original, meaning if it's a 7 digit value, I don't have to have 7 x's, I can replace the value, whatever the length, with say 7 or 10 x's. I'm just not sure how to identify the original variable length string.

Any thoughts?
# 2  
Old 02-06-2009
Tools Would this work?

Code:
> echo "args=123456789&acctnum=123456789"
args=123456789&acctnum=123456789

> echo "args=123456789&acctnum=123456789" | tr "[:digit:]" "X"
args=XXXXXXXXX&acctnum=XXXXXXXXX

# 3  
Old 02-06-2009
sed -e "s/args.*acctnum.*/args=XXXXXXXXX&acctnum=XXXXXXXXX/g" file > file.out

or

perl -i -pe "s/args.*acctnum.*/args=XXXXXXXXX&acctnum=XXXXXXXXX/g" file
# 4  
Old 02-06-2009
Quote:
Originally Posted by djp
sed -e "s/args.*acctnum.*/args=XXXXXXXXX&acctnum=XXXXXXXXX/g" file > file.out

or

perl -i -pe "s/args.*acctnum.*/args=XXXXXXXXX&acctnum=XXXXXXXXX/g" file
Code:
$ echo 'args=123456789&acctnum=123456789'|sed -e "s/args.*acctnum.*/args=XXXXXXXXX&acctnum=XXXXXXXXX/g"
args=XXXXXXXXXargs=123456789&acctnum=123456789acctnum=XXXXXXXXX

# 5  
Old 02-06-2009
Man, thanks for the quick replies everyone. I haven't played with any of them yet, I will after this post. But I wanted to give you the entire picture, just in case it helps, sorry should have put this in the orig post.

Here is a single entry from the log (edited of course)
Code:
www.testsite.org 11.222.333.444 - - [02/Feb/2009:13:58:18 -0600] "GET /regrenew/pay.cgi?fname=complete_payment&args=John%20C.%20Doe&acctname=John
%20C.%20Doe&args=2468%20Jackson&acctaddress=2468%20Jackson&args=Miami&acctcity=Miami&args=FL&acctstate=FL&args=12345&acctzip=12345&args=111111111
&routing=111111111&args=2222222222&acctnum=2222222222&args=jdoe@email.net&acctemail=jdoe@email.net&args=999-999-9999&acctphone=999-999-9999 HTTP/
1.1" 200 1996 "https://www.testsite.org/regrenew/index.cgi" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 
2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.04506.648; Windows-Media-Player/10.00.00.3990; Zune 3.0)"

I just need to x out this section
Code:
args=111111111&routing=111111111&args=2222222222&acctnum=2222222222

I need to keep all other entries, phone, address, zip, etc.

Again thanks a ton for the quick replies, I'm going to go test them now.
# 6  
Old 02-06-2009
if the number of X-s doesn't have to match the number of the original digits:
Code:
sed 's#args=[0-9][0-9]*&routing=[0-9][0-9]*&args=[0-9][0-9]*&acctnum=[0-9][0-9]*#args=xxxxxxxx\&routing=xxxxxxxx\&args=xxxxxxxx\&acctnum=xxxxxxxx#' myFile

# 7  
Old 02-06-2009
And the winner is......

vgersh99!!

I started testing with the most recent posts and low and behold the last one from vgersh99 worked perfectly.

Many thanks to all of you for your efforts.
Vgersh99 if I could send you the beverage of your choice I would, but in this
medium you'll have to settle for a Smilie Smilie

Have a great day everyone!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Views How to replace a CRLF char from a variable length file in the middle of a string in UNIX?

My sample file is variable length, with out any field delimiters. It has min of 18 chars length and the 'CRLF' is potentially between 12-14 chars. How do I replace this with a space? I still want to keep end of record, but just want to remove these new lines chars in the middle of the data. ... (7 Replies)
Discussion started by: chandrath
7 Replies

2. Shell Programming and Scripting

Need to replace 32 length string to *****

Hello, I've a file file_1.txt ul.ln = 'Kb' AND (il.sum = 'ec7ccc92585adac28d9b1fb5eff0473f' or length('ec7ccc92585adac28d9b1fb5eff0473f') = 7); il.sum = '20ddc1368d588dd0b0e37c41721e05b1' OR length('20ddc1368d588dd0b0e37c41721e05b1') = 7); I need to replace the file like below - ul.ln... (4 Replies)
Discussion started by: Mannu2525
4 Replies

3. Shell Programming and Scripting

Extracting substrings from a string of variable length

I have a string like Months=jan feb mar april x y .. Here the number of fields in Months is not definite I need to extract each field in the Months string and pass it to awk . Don't want to use for in since it is a loop . How can i do it (2 Replies)
Discussion started by: Nevergivup
2 Replies

4. Shell Programming and Scripting

Match and variable length string

Hello all, source file looks like this: cat toto.txt NAME) VAR1=tata OPT4=toto USER=blabla TZ=/usr/share/zoneinfo/Hongkong OTHEROPT=something ;; NAME2) VAR1=tata OPT4=toto USER=blabla TZ=/usr/share/zoneinfo/Hongkong SOMETHING=else ... (5 Replies)
Discussion started by: maverick72
5 Replies

5. Shell Programming and Scripting

String variable to numeric conversion in perl

Hi guys I am having this strange issue.Well my requirement is like below Compare two values between flat file and oracle DB Via perl script I am easily getting the rowcount Now I connect sql plus via perl and the column value that returns is string my $sqlplus_settings = ''; my... (7 Replies)
Discussion started by: Pratik4891
7 Replies

6. UNIX for Dummies Questions & Answers

Find and Replace random numeric value with non-numeric value

Can someone tell me how to change the first column in a very large 17k line file from a random 10 digit numeric value to a non numeric value. The format of lines in the file is: 1702938475,SNU022,201004 the first 10 numbers always begin with 170 (6 Replies)
Discussion started by: Bahf1s
6 Replies

7. Shell Programming and Scripting

Replacing one Char in a string of variable length

Hi all, I am trying to find the best way of making a change to 1 char in a string, the string can be between 1 and 14 characters. I am reading a line in from a file which contains 012341231231:2:102939283:NNN: Require :NBN: 012838238232:3:372932:NNN: Require :NNB: I need to change 1 N or a... (8 Replies)
Discussion started by: nkwilliams
8 Replies

8. Shell Programming and Scripting

awk - replace number of string length from search and replace for a serialized array

Hello, I really would appreciate some help with a bash script for some string manipulation on an SQL dump: I'd like to be able to rename "sites/WHATEVER/files" to "sites/SOMETHINGELSE/files" within the sql dump. This is quite easy with sed: sed -e... (1 Reply)
Discussion started by: otrotipo
1 Replies

9. Shell Programming and Scripting

numeric string and length

given a string passed to a program that supposed to be numeric and of a certain length say 8 digits - so say for e.g. need to verify this 01234567 How would I parse this string to validat it meet requirements I tried to use * | sed /\(\{8})/ Thanks in advance (1 Reply)
Discussion started by: dragrid
1 Replies

10. Shell Programming and Scripting

Finding out the length of a string held within a variable

:confused: Does anyone know which command I can use to find out the length of a string held within a variable? (5 Replies)
Discussion started by: dbrundrett
5 Replies
Login or Register to Ask a Question