Solved

Searching and Replacing strings using SED command

Posted on 2004-10-04
3
633 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 40

Accepted Solution

by:
jlevie earned 50 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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.

630 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