Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

The file contains:


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:


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:

share|improve this question
up vote 4 down vote accepted

Here's the answer to your question:


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


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


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

share|improve this answer
$ 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.

share|improve this answer
sed '/\(dateukfilename\)\(..\)\(..\)\(....\)/s//\1\4\3\2/'
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.