# Replace variable length numeric string

**URL:** <https://community.unix.com/t/replace-variable-length-numeric-string/230656>\
**Category:** Shell Programming and Scripting\
**Created:** [February 6, 2009, 11:55am UTC](https://community.unix.com/t/replace-variable-length-numeric-string/230656 "2009-02-06T11:55:19Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mk4mzid](https://community.unix.com/letter_avatar/mk4mzid/32/6_449c533f586939b6e5f246632a46421f.png) [@mk4mzid](https://community.unix.com/u/mk4mzid)\
**Post date:** [February 6, 2009, 11:55am UTC](https://community.unix.com/t/replace-variable-length-numeric-string/230656/1 "2009-02-06T11:55:19Z")

</div>

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

```nohighlight
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

```nohighlight
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?

---

<div class="post-metadata">

**Author:** ![joeyg](https://community.unix.com/user_avatar/community.unix.com/joeyg/32/780_2.png) [@joeyg](https://community.unix.com/u/joeyg)\
**Post date:** [February 6, 2009, 12:02pm UTC](https://community.unix.com/t/replace-variable-length-numeric-string/230656/2 "2009-02-06T12:02:12Z")

</div>

```nohighlight
> echo "args=123456789&acctnum=123456789"
args=123456789&acctnum=123456789

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

```

---

<div class="post-metadata">

**Author:** ![djp](https://community.unix.com/user_avatar/community.unix.com/djp/32/1147_2.png) [@djp](https://community.unix.com/u/djp)\
**Post date:** [February 6, 2009, 12:09pm UTC](https://community.unix.com/t/replace-variable-length-numeric-string/230656/3 "2009-02-06T12:09:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [February 6, 2009, 12:15pm UTC](https://community.unix.com/t/replace-variable-length-numeric-string/230656/4 "2009-02-06T12:15:38Z")

</div>

> [@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

```plaintext
$ echo 'args=123456789&acctnum=123456789'|sed -e "s/args.*acctnum.*/args=XXXXXXXXX&acctnum=XXXXXXXXX/g"
args=XXXXXXXXXargs=123456789&acctnum=123456789acctnum=XXXXXXXXX

```

---

<div class="post-metadata">

**Author:** ![mk4mzid](https://community.unix.com/letter_avatar/mk4mzid/32/6_449c533f586939b6e5f246632a46421f.png) [@mk4mzid](https://community.unix.com/u/mk4mzid)\
**Post date:** [February 6, 2009, 12:29pm UTC](https://community.unix.com/t/replace-variable-length-numeric-string/230656/5 "2009-02-06T12:29:43Z")

</div>

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)

```nohighlight
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

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

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [February 6, 2009, 1:04pm UTC](https://community.unix.com/t/replace-variable-length-numeric-string/230656/6 "2009-02-06T13:04:00Z")

</div>

if the number of X-s doesn't have to match the number of the original digits:

```nohighlight
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

```

---

<div class="post-metadata">

**Author:** ![mk4mzid](https://community.unix.com/letter_avatar/mk4mzid/32/6_449c533f586939b6e5f246632a46421f.png) [@mk4mzid](https://community.unix.com/u/mk4mzid)\
**Post date:** [February 6, 2009, 2:07pm UTC](https://community.unix.com/t/replace-variable-length-numeric-string/230656/7 "2009-02-06T14:07:59Z")

</div>

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 :b: 😃

Have a great day everyone!!

---

<div class="post-metadata">

**Author:** ![djp](https://community.unix.com/user_avatar/community.unix.com/djp/32/1147_2.png) [@djp](https://community.unix.com/u/djp)\
**Post date:** [February 6, 2009, 4:13pm UTC](https://community.unix.com/t/replace-variable-length-numeric-string/230656/8 "2009-02-06T16:13:34Z")

</div>

> [@vgersh99](#):
>
> ```plaintext
> $ echo 'args=123456789&acctnum=123456789'|sed -e "s/args.*acctnum.*/args=XXXXXXXXX&acctnum=XXXXXXXXX/g"
> args=XXXXXXXXXargs=123456789&acctnum=123456789acctnum=XXXXXXXXX
> 
> ```

Sorry, my mistake, should have been:

sed -e "s/args.\*acctnum.\*/args=XXXXXXXXX\&acctnum=XXXXXXXXX/g"
