• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 640
  • Last Modified:

Searching and Replacing strings using SED command


I am trying to rearrange this string:  Pooh:bear:honey:5/14/1942 so that is outputed this way:  

This is the command I am using:

sed "s/^\(.*\):\(.*\)$/\2:\1/" aamilne.org >> test1

where aamilne.org is the orginal file with the strings and I am redirecting the new output into a test file before amending it to the correct file.

Using this command my output is:  5/14/1942:Pooh:bear:honey

Can someone please help me.  Also if anyone knows of a good website to help me with this matter as well.  I am confused on using the regular expressions.

Thank you to all.
  • 2
1 Solution
Use sed -e "s/\(.*\):\(.*\):\(.*\):\(.*\)$/\4:\2:\3:\1/"

It is usually a mistake to anchor both ends of a regex (^ & $). A better approach is to figure out which end is really important and anchor it. The example above splits the string on the ':'s and then outputs them in the desired order.
dabratAuthor Commented:
Thanks for the help, do you know of any good website to help me on this, i find this part of unix to be the most confusing, thanks again
Try 'man 7 regex'. It will require a few readings and experimentation, but you'll soon get the hang of it.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now