Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 05-18-2012
Registered User
 
Join Date: Mar 2012
Posts: 49
Thanks: 22
Thanked 0 Times in 0 Posts
Selective grep

I have to grep out only email address from a column. It has characters appended and prepended


Code:
 F=<sss1@domain.com>
 <sss2@domain.com>
(sss3@domain.com)
 <sss4@domain.com>

Whatever added before and after email, I should be able to grep out only emails.
Sponsored Links
    #2  
Old 05-18-2012
guruprasadpr's Avatar
Shrink...ing
 
Join Date: Jun 2009
Location: India
Posts: 674
Thanks: 30
Thanked 215 Times in 214 Posts
Hi



Code:
$ grep -o '[[:alnum:]]*@[[:alpha:]]*\.com' file
sss1@domain.com
sss2@domain.com
sss3@domain.com
sss4@domain.com

Guru.
The Following User Says Thank You to guruprasadpr For This Useful Post:
anil510 (05-18-2012)
Sponsored Links
    #3  
Old 05-18-2012
Registered User
 
Join Date: Mar 2012
Posts: 49
Thanks: 22
Thanked 0 Times in 0 Posts
Thank you guruprasadpr,

If there are mixed tlds, like .com, .net, .co.in, .in etc..
    #4  
Old 05-18-2012
balajesuri's Avatar
#! /bin/bash
 
Join Date: Apr 2009
Location: India
Posts: 1,565
Thanks: 14
Thanked 440 Times in 425 Posts
A slight extension to guruprasadpr's solution:


Code:
grep -Eo '[[:alnum:]]*@[[:alpha:]]*(\.[a-z]{2,4})+' file

Sponsored Links
    #5  
Old 05-18-2012
itkamaraj's Avatar
^Kamaraj^
 
Join Date: Apr 2010
Posts: 3,025
Thanks: 33
Thanked 647 Times in 625 Posts

Code:
 
$ nawk -F"[<>()]" '{print $2}' test.txt
sss1@domain.com
sss2@domain.com
sss3@domain.com
sss4@domain.com

Sponsored Links
    #6  
Old 05-18-2012
complex.invoke's Avatar
Registered User
 
Join Date: Nov 2009
Location: BeiJing China
Posts: 234
Thanks: 6
Thanked 17 Times in 8 Posts

Code:
sed 's/.*[<(]\([^>)]*\)[>)]/\1/g' infile

Sponsored Links
    #7  
Old 05-18-2012
Registered User
 
Join Date: Mar 2012
Posts: 49
Thanks: 22
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by balajesuri View Post
A slight extension to guruprasadpr's solution:


Code:
grep -Eo '[[:alnum:]]*@[[:alpha:]]*(\.[a-z]{2,4})+' file


Code:
# cat  /root/gmail.txt
now_u.k12@gmail.com
c.gg@gmail.com
s_klk@gmail.com

When _ or . character is in email, it gives wrong result.

Code:
# cat /root/gmail.txt   |  grep -o '[[:alnum:]]*@gmail.com' |sort|uniq -c|sort -nk 1
      1 gg@gmail.com
      1 k12@gmail.com
      1 klk@gmail.com

How to solve this?
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Help with selective ls newbie_01 UNIX for Dummies Questions & Answers 7 04-03-2012 07:58 AM
selective grep verse123 UNIX for Dummies Questions & Answers 6 01-24-2012 10:19 AM
selective printing JoeColeEPL9 Shell Programming and Scripting 4 09-22-2011 08:02 AM
Selective Umask baanprog UNIX for Advanced & Expert Users 3 08-03-2006 09:48 AM
Using `tar` for a selective backup. Cameron Filesystems, Disks and Memory 2 07-16-2002 09:10 AM



All times are GMT -4. The time now is 01:51 AM.