Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
08-22-2006, 09:14 AM
|
#1
|
|
LQ Newbie
Registered: Aug 2006
Posts: 2
Rep:
|
Help with sed - replacing strings
I have an alias table and I need to make a global change due to a new mail server. I think I can do it with sed, but don't know how.
a sample line look like this
skrutua06: sanhuda_krutua06@firstclass.choate.edu
I need to change the format to firstinitialLASTNAME
and change the mail server like this
skrutua06: skrutua06@newserver.choate.edu
How can I strip the characters before the underscore and replace it with just the first initial. I know how to simply change the server name after the @ sign. that part was easy. there will be a variable length string for each first name but they will all have a space or whitespace character after the semicolon.
Thanks in advance.
|
|
|
|
08-22-2006, 09:36 AM
|
#2
|
|
LQ Veteran
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 8,695
|
Hi,
This should work:
sed -e 's/ \([a-z]\)[a-z]*_/ \1/' -e 's/firstclass/newserver/' <infile>
The first substitution takes everything from the first space to the underscore and replaces it with the content of \([a-z]\), this is done with the \1 in the replace part.
With the -e option you can string sed statement together.
The second one should look familiar.
Hope this helps.
|
|
|
|
08-22-2006, 09:55 AM
|
#3
|
|
Senior Member
Registered: Oct 2003
Posts: 3,057
Rep:
|
This might work also...
Code:
sed -e 's/[a-z]*_//g' file.txt
|
|
|
|
08-22-2006, 10:00 AM
|
#4
|
|
LQ Newbie
Registered: Aug 2006
Posts: 2
Original Poster
Rep:
|
Fantastic.
Thanks, you saved me lots of time.
It works perfectly.
Tom
Quote:
|
Originally Posted by druuna
Hi,
This should work:
sed -e 's/ \([a-z]\)[a-z]*_/ \1/' -e 's/firstclass/newserver/' <infile>
The first substitution takes everything from the first space to the underscore and replaces it with the content of \([a-z]\), this is done with the \1 in the replace part.
With the -e option you can string sed statement together.
The second one should look familiar.
Hope this helps.
|
|
|
|
|
08-22-2006, 10:07 AM
|
#5
|
|
LQ Veteran
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 8,695
|
Hi,
@thulley: You're welcome
@homey: Your given command deletes every character before, and including, the underscore. It does not leave the first character of that string, as desired by the OP.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 08:41 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|