LinuxQuestions.org
Review your favorite Linux distribution.
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 05-12-2009, 10:57 AM   #1
rjkfsm
Member
 
Registered: Apr 2004
Location: Charleston, SC
Distribution: RHEL, CentOS, Debian, Gentoo, Knoppix & DSL
Posts: 126

Rep: Reputation: 15
sed help plz


I am looking to use sed in a script. I have searched through the docs and cannot find what I am looking for.

If I have an email address like frustrated_sed_user@smtp1.mail.linux.com. How do I get the topmost domain out? (ie linux.com)

RK
 
Old 05-12-2009, 11:01 AM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
awk is a better tool for extracting fields:

Code:
echo frustrated_sed_user@smtp1.mail.linux.com |awk -F. '{print $3"."$4}'
The "-F." is telling awk to use dot as the field separator instead of white space. The dot in quotes in the print section is adding the dot back to the output as awk stripped it out when it broke the fields up. Fields 3 and 4 are being printed before and after the dot respectively.

Last edited by MensaWater; 05-12-2009 at 11:03 AM.
 
Old 05-12-2009, 11:36 AM   #3
rjkfsm
Member
 
Registered: Apr 2004
Location: Charleston, SC
Distribution: RHEL, CentOS, Debian, Gentoo, Knoppix & DSL
Posts: 126

Original Poster
Rep: Reputation: 15
Well, I can tell I'm warmer...

How do I do a reverse search?

If I do:
Code:
echo frustrated_sed_user@smtp1.mail.linux.com |awk -F. '{print $3"."$4}'
I get linux.com just like I want, but if I have:
Code:
echo frustrated_sed_user@smtp1.linux.com |awk -F. '{print $3"."$4}'
I get com.

Further clarification: I'm looking for a more general email address filter, not something that only works with that one string.

Oh and thank you very much for your reply. I think awk may be what I need, but geez that's a lot of documentation.

RK
 
Old 05-12-2009, 11:42 AM   #4
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
Code:
echo frustrated_sed_user@smtp1.mail.linux.com |awk -F. '{print $(NF-1)"."$NF}'
NF = number of fields so $NF would be last field and $(NF-1) would be field before last field. So long as you have at least 2 fields it should work for any address that ends in the domain you want.
 
Old 05-12-2009, 02:12 PM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Really good tutorials here---SED, AWK, and more:
http://www.grymoire.com/Unix/

Beats reading man pages.....
 
Old 05-12-2009, 04:55 PM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,188

Rep: Reputation: 4131Reputation: 4131Reputation: 4131Reputation: 4131Reputation: 4131Reputation: 4131Reputation: 4131Reputation: 4131Reputation: 4131Reputation: 4131Reputation: 4131
Being not well versed in awk, I only use it when data is (extremely) well structured - as in the cases above.
Must admit I have a leaning toward regex in that it can be used to extract data from anywhere in a record - building a (fool-proof) regex for this could get challenging though.
Perl might be a better option than sed.
 
Old 05-12-2009, 06:56 PM   #7
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 360

Rep: Reputation: 170Reputation: 170
Another method
Code:
echo '
frustrated_sed_user@smtp1.mail.linux.com
frustrated_sed_user@smtp1.linux.com
frustrated_sed_user@linux.com' | grep -o '[^.@]*\.[^.]*$'

linux.com
linux.com
linux.com
 
Old 05-13-2009, 05:36 AM   #8
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Quote:
Originally Posted by rjkfsm View Post
Well, I can tell I'm warmer...

How do I do a reverse search?

If I do:
Code:
echo frustrated_sed_user@smtp1.mail.linux.com |awk -F. '{print $3"."$4}'
I get linux.com just like I want, but if I have:
Code:
echo frustrated_sed_user@smtp1.linux.com |awk -F. '{print $3"."$4}'
I get com.

Further clarification: I'm looking for a more general email address filter, not something that only works with that one string.

Oh and thank you very much for your reply. I think awk may be what I need, but geez that's a lot of documentation.

RK
I wouldn't use awk here, but you can. Here's how I would do it to make it more useful:

Code:
bash-3.1$ echo frustrated_sed_user@smtp1.mail.linux.com | rev | cut -d . -f 1
moc
bash-3.1$ echo frustrated_sed_user@smtp1.mail.linux.com | rev | cut -d . -f 1 | rev
com
bash-3.1$ echo frustrated_sed_user@smtp1.mail.linux.com | rev | cut -d . -f 2 | rev
linux
bash-3.1$ echo frustrated_sed_user@smtp1.mail.linux.com | rev | cut -d . -f 1-2 | rev
linux.com
bash-3.1$ echo frustrated_sed_user@smtp1.linux.com | rev | cut -d . -f 1-2 | rev
linux.com
So basically using 'rev' is a good idea here. It reverses lines character by character.

Last edited by H_TeXMeX_H; 05-13-2009 at 05:38 AM.
 
Old 05-13-2009, 06:35 AM   #9
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by H_TeXMeX_H View Post
I wouldn't use awk here, but you can. Here's how I would do it to make it more useful:
but here, you make extra calls to rev, cut.
 
Old 05-13-2009, 06:39 AM   #10
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by syg00 View Post
I have a leaning toward regex in that it can be used to extract data from anywhere in a record - building a (fool-proof) regex for this could get challenging though.
well, i don't know why you think without regex you can't extract data from anywhere in a record
 
Old 05-13-2009, 07:12 AM   #11
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Quote:
Originally Posted by ghostdog74 View Post
but here, you make extra calls to rev, cut.
The only extra call is to 'rev'. You can use awk instead of cut:

Code:
bash-3.1$ echo frustrated_sed_user@smtp1.mail.linux.com | rev | awk -F. '{print $1"."$2}'| rev
linux.com
Or you could use just awk:

Code:
bash-3.1$ echo frustrated_sed_user@smtp1.mail.linux.com | awk -F. '{print $(NF-1)"."$NF}'
linux.com
as jlightner said earlier

NF is the number of fields, so NF is the last field, and NF-1 is the next to last field.

Whichever way you want to do it, there are so many ways.

Last edited by H_TeXMeX_H; 05-13-2009 at 09:22 AM.
 
Old 05-13-2009, 07:19 AM   #12
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by H_TeXMeX_H View Post
The only extra call is to 'rev'. You can use awk instead of cut:

Code:
bash-3.1$ echo frustrated_sed_user@smtp1.mail.linux.com | rev | awk -F. '{ print $1"."$2}'| rev
linux.com
still the same thing. you have to call rev 2 times, awk 1 time. See post #4 by jlightner. that's common way to get awk fields from the back.

Code:
# time echo frustrated_sed_user@smtp1.mail.linux.com |awk -F. '{print $(NF-1)"."$NF}'
linux.com

real    0m0.004s
user    0m0.004s
sys     0m0.000s

# time echo frustrated_sed_user@smtp1.mail.linux.com | rev | awk -F. '{ print $1"."$2}'| rev
linux.com

real    0m0.007s
user    0m0.004s
sys     0m0.004s
 
Old 05-13-2009, 07:23 AM   #13
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Oh yeah, I guess I missed that post, and posted the same thing just a second ago. Oh whatever, 0.003 sec is that important.
 
Old 05-13-2009, 07:28 AM   #14
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by H_TeXMeX_H View Post
Whichever way you want to do it, there are so many ways.
yes, there are many ways, but don't choose the ones less obvious.
 
Old 05-13-2009, 09:10 AM   #15
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
My second post did it without having to "rev" anything. I just changed the variables to be relative to number of fields rather than explicit 3 and 4.
 
  


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
plz plz help me regarding route mapping nedian123 Programming 1 07-13-2004 08:17 AM
plz plz solve my route mapping problem nedian123 Linux - Networking 1 07-12-2004 09:41 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM
redhat problems plz plz help sap666 Linux - Newbie 5 07-30-2003 10:57 AM
help this to make this sed efficient plz doublefailure Programming 1 03-25-2002 06:57 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 04:56 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration