LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-05-2013, 02:04 PM   #1
socalheel
Member
 
Registered: Oct 2012
Location: Raleigh, NC
Distribution: CentOS / RHEL
Posts: 158

Rep: Reputation: 3
extract string from right to left


my goal is to only get the domain from an email address.

my problem is there is no standardization to an email address to where there are an equal amount of " . " in the address. is there a way i can extract just the last two words from a string without using "rev"?

example -
from this output:
username@domain1.com
51arouff7n19wbhr@qrqu6wz38tug.a-byiu...nc.domain2.com

i only want to extract
domain1.com
domain2.com
 
Old 09-05-2013, 02:27 PM   #2
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
Code:
awk  'BEGIN{FS="[.@]"};{print $(NF-1)"."$NF}'
set Field separators to . and @
print the second to last and last field ( with a . between them )

NF is awk internal var for Number of Fields
 
Old 09-05-2013, 02:29 PM   #3
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Or even shorter:
Code:
awk -F[.@] '{print $(NF-1)"."$NF}' infile
EDIT:

perl alternative:
Code:
perl -ne 'print "$1\n" if /(\w+\.\w+)$/'

Last edited by firstfire; 09-05-2013 at 02:31 PM.
 
Old 09-05-2013, 02:35 PM   #4
socalheel
Member
 
Registered: Oct 2012
Location: Raleigh, NC
Distribution: CentOS / RHEL
Posts: 158

Original Poster
Rep: Reputation: 3
awesome stuff guys ... thank you so much!

worked like a champ.
 
  


Reply



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
[SOLVED] left shift to remove string niteen Programming 16 04-02-2013 05:21 AM
how to extract from a string Owais.Ahmad Linux - Newbie 13 07-27-2012 06:13 AM
extract a string within a string using a pattern adshocker Linux - Newbie 1 11-04-2010 10:44 PM
Extract value from string ewingtux Programming 1 12-27-2008 07:19 PM
Extract part of a string steven.c.banks Linux - General 7 05-07-2008 07:18 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:26 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