LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Help with sed - replacing strings (https://www.linuxquestions.org/questions/linux-software-2/help-with-sed-replacing-strings-476225/)

thulley 08-22-2006 09:14 AM

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.

druuna 08-22-2006 09:36 AM

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.

homey 08-22-2006 09:55 AM

This might work also...
Code:

sed -e 's/[a-z]*_//g' file.txt

thulley 08-22-2006 10:00 AM

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.


druuna 08-22-2006 10:07 AM

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.


All times are GMT -5. The time now is 09:16 AM.