Christoph
Christoph

Reputation: 89

Using Sed to replace parenthese (and contents) with a period

I have DNS logs that have the domain name in this format (3)www(6)google(3)com(0). I need that to be www.google.com instead. This Sed command will replace the parentheses and contents with a period.

s/([0-9])/./g 

.www.google.com.

Now I need to remove the leading and trailing period. Luckily the trailing period is at the end of the line so I can use this to remove it.

s/.$//g 

.www.google.com

Is there a way I can remove the leading period without affecting any other periods? Unfortunately the leading period is not at the beginning of the line. A line looks like this:

10/09/2013 08:15:57 0B58 PACKET  000000000ED9F540 UDP Rcv 10.35.83.5      c664   Q [0001   D   NOERROR] A      (3)crl(9)microsoft(3)com(0)

Is there a better way to to do this?

Upvotes: 0

Views: 118

Answers (2)

Hari Menon
Hari Menon

Reputation: 35405

First replace the first occurrence of ([0-9]) with an empty string.

sed s/([0-9])//  ## no /g at the end!

Now do sed s/([0-9])/./g and sed s/.$//g as you were doing earlier. This will work since you are already assuming that all the ([0-9]) occurs only in the url.

For clarity, you can combine these sed operations like:

sed 's/([0-9])//' -e 's/([0-9])/./g' -e 's/.$//g' <filename>

Upvotes: 1

Michael J. Barber
Michael J. Barber

Reputation: 25042

Using sed, you can specify the match to substitute by giving a number. Thus, you can use:

sed -e 's/([[:digit:]])/./g' -e 's/\.$//' -e 's/\.//4'

This assumes that the lines in the log are predictable enough to be sure that the dot you want to remove is the first one following an IP address.

Upvotes: 0

Related Questions