tags:

views:

44

answers:

3

I have a lot lines contains XXXXXXXXX-XXX.XXX number format. I want change number XXXXXXXXX-XXX.XXX to XX.XXX.XXX.X-XXX.XXX

XXXXXXXXX-XXX.XXX = 15 digit random number

Anyone can help me? Thanks in advance

+2  A: 

General regex: Search for (\d{2})(\d{3})(\d{3})(\d)-(\d{3}\.\d{3}) and replace with \1.\2.\3.\4-\5.

I don't know sed well enough, but I think the syntax there is a bit different. Try this:

sed 's/\([0-9]\{2\}\)\([0-9]\{3\}\)\([0-9]\{3\}\)\([0-9]\)-\([0-9]\{3\}\.[0-9]\{3\}\)/\1.\2.\3.\4-\5/')
Tim Pietzcker
A: 

Try this:

 echo "987654321" | sed 's/\([0-9][0-9]\)\([0-9][0-9][0-9]\)\([0-9][0-9][0-9]\)\([0-9]\)/\1.\2.\3.\4/'

(this outputs 98.765.432.1)

Then, for reading input from a file:

sed 's/\([0-9][0-9]\)\([0-9][0-9][0-9]\)\([0-9][0-9][0-9]\)\([0-9]\)/\1.\2.\3.\4/' in.txt

and, for writing to a new file:

sed 's/\([0-9][0-9]\)\([0-9][0-9][0-9]\)\([0-9][0-9][0-9]\)\([0-9]\)/\1.\2.\3.\4/' in.txt > out.txt

I guess you could refine it, but that's the general idea.

amir75
Ah sorry, I mis-read. Tim's answer is correct. You get the idea ...
amir75
A: 

This is ugly (because sed uses POSIX regex), but should work:

sed 's/\([0-9]\{2\}\)\([0-9]\{3\}\)\([0-9]\{3\}\)\([0-9]\)\(-[0-9]\{3\}\.[0-9]\{3\}\)/\1.\2.\3.\4\5/g'
soulmerge