I have a lo开发者_StackOverflow社区t 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
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/')
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'
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.
精彩评论