2

The file contains:

dateutkfilename25012009

I want to change the position of character 16th to 17th with 18th to 19th. And then change position of character 16th to 19th with 20th to 23rd... so it will be:

dateutkfilename20090125

I've tried to change position of character 16th to 17th with 18th to 19th using below code, but I don't understand why it not works:

'/dateutkfilename/s/\(.\{16\}\)\([0-9]\{2\}\)\(.*\)/\(.\{18\}\)\([0-9]\{2\}\)\(.*\)/g'

3 Answers 3

4

Here's the answer to your question:

s/^\(.\{15\}\)\(.\{2\}\)\(.\{2\}\)\(.\{4}\)/\1\4\3\2/

But if you can anchor to the end instead, it gets simpler:

s/\(.\{2\}\)\(.\{2\}\)\(.\{4\}\)$/\3\2\1/

Personally, I'd probably do [0-9] instead of . as well:

s/\([0-9]\{2\}\)\([0-9]\{2\}\)\([0-9]\{4\}\)$/\3\2\1/

As usual, there's more than one way to do it.

1
$ sed -e 's/\(.\{15\}\)\([0-9]\{2\}\)\([0-9]\{2\}\)\([0-9]\{4\}\)/\1\4\3\2/g' /tmp/test.txt

seems to do what you are expecting.

0
sed '/\(dateukfilename\)\(..\)\(..\)\(....\)/s//\1\4\3\2/'

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.