Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Searching and Replacing strings using SED command

Posted on 2004-10-04
3
Medium Priority
?
639 Views
Last Modified: 2010-04-21
Hello,

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

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.
0
Comment
Question by:dabrat
  • 2
3 Comments
 
LVL 40

Accepted Solution

by:
jlevie earned 200 total points
ID: 12221014
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.
0
 
LVL 1

Author Comment

by:dabrat
ID: 12222012
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
0
 
LVL 40

Expert Comment

by:jlevie
ID: 12222768
Try 'man 7 regex'. It will require a few readings and experimentation, but you'll soon get the hang of it.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Suggested Courses

927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question