Solved

Searching and Replacing strings using SED command

Posted on 2004-10-04
3
606 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
non-interactive authentication of sftp command in AIX 5.3 15 187
unix scripting question 1 89
Invalid password trying to cannect to Unix server 2 69
Shell Script on AIX 7 63
Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
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…
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…

910 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

20 Experts available now in Live!

Get 1:1 Help Now