LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
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

Reply
 
Search this Thread
Old 08-22-2006, 09:14 AM   #1
thulley
LQ Newbie
 
Registered: Aug 2006
Posts: 2

Rep: Reputation: 0
Question 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.
 
Old 08-22-2006, 09:36 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373
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.
 
Old 08-22-2006, 09:55 AM   #3
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
This might work also...
Code:
sed -e 's/[a-z]*_//g' file.txt
 
Old 08-22-2006, 10:00 AM   #4
thulley
LQ Newbie
 
Registered: Aug 2006
Posts: 2

Original Poster
Rep: Reputation: 0
Smile 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.
 
Old 08-22-2006, 10:07 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373Reputation: 2373
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Replacing a place holder on a template with bash, sed, and or awk rignes Programming 7 02-16-2006 04:20 PM
how to find duplicate strings in vertical column of strings markhod Programming 7 11-02-2005 04:04 AM
sed help, replacing a letter with a word GridX Linux - Newbie 1 09-24-2003 10:21 AM
Replacing String with File Content in Sed meshcurrent Linux - General 2 06-01-2003 12:54 AM
replacing pattern with sed produces double realos Programming 1 10-17-2002 08:03 PM


All times are GMT -5. The time now is 10:43 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration