Solved

Searching and Replacing strings using SED command

Posted on 2004-10-04
3
596 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 50 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Try 'man 7 regex'. It will require a few readings and experimentation, but you'll soon get the hang of it.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

762 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now