Sponsored Content
Top Forums Shell Programming and Scripting SED With Regex to extract Email Address Post 302618855 by ctsgnb on Wednesday 4th of April 2012 02:25:01 PM
Old 04-04-2012
Just feel free to adjust with some additionnal [A-Z] range or whatever but here is an idea :

(The trick is to add a space at the beginning of a line so that if the line contains only the mail address, the next regular expression used to reach the mail adresse preceeded by a space would also match.)

Code:
echo "Email address is abc.xyz@comp.com" | sed 's/.*/ &/;s/.* \([^ @]*@[^ @]*.com\).*/\1/'

(Just remember that if you want the hyphen to be taken as litteral in a list it should be set at the end of it

quick example : [:#@-]


also consider :
Code:
... | sed 's/.*/ &/;s/.* \([A-Za-z0-9.-]*@[A-Za-z0-9.-]*.com\).*/\1/'

but this would also match an adress with empty user and/or domain like : @.com

so you want it to be at least one char (and you don't want it to be a an hyphen or a dot you can for example:
Code:
... | sed 's/.*/ &/;s/.* \([A-Za-z0-9][A-Za-z0-9.-]*@[A-Za-z0-9][A-Za-z0-9.-]*.com\).*/\1/'

Just adjust it to your requirements

If you know that the mail address always appear at the end of line and after a space you can simply :

Code:
echo "Email address is abc.xyz@comp.com" | sed 's/.* //g'

---------- Post updated at 08:25 PM ---------- Previous update was at 07:55 PM ----------

Code:
# cat tst
first-name1.last-name@xyz.com
bla bla first-name2.last-name@xyz.com blebla blealdsfl
first-name3.last-name@xyz.com is a valid mail address.com
This line with valid adress.com: first-name4.last-name@xyz.com next nested address.com
last valid mail address.com first-name5.last-name@xyz.com
# sed 's/.*/ &/;s/.* \([^ @]*@[^ @]*.com\).*/\1/' tst
first-name1.last-name@xyz.com
first-name2.last-name@xyz.com
first-name3.last-name@xyz.com
first-name4.last-name@xyz.com
first-name5.last-name@xyz.com

(Ok i didn't handle the case where the separator is a tabulation instead of a space but it's easy to tweak the code or to tr -s '[:blank:]' ' ' <output before applying the sed statement

Last edited by ctsgnb; 04-04-2012 at 03:32 PM..
 

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Send email where # is in the email address - Using Unix

Hi All, How do I send an email using malix where email address contains a #. I have a email address like this : #test@test.com I want to send email like malix -s "TEST" #test@test.com < SOMEFILE I tried \# but doesn't work. Please let me know how we can achieve this? I am in... (1 Reply)
Discussion started by: jingi1234
1 Replies

2. UNIX for Advanced & Expert Users

Regex to match IP address

What do you think of this regex to match IP address? I have been reading up on regex and have seen some really long ones for IP. Would this fail in any scenarios? (+\.){3}* (5 Replies)
Discussion started by: glev2005
5 Replies

3. Shell Programming and Scripting

Using SED command in a shell script: Unterminated address regex

Hi All, I am trying to use a sed command in a shell script in order to delete some lines in a file and I got the following error message. I don't understand why it is not working 'cause I have tried with simple quotes, then with double-quotes, and it is not working. sed: -e expression #1,... (7 Replies)
Discussion started by: Alpha3363
7 Replies

4. Shell Programming and Scripting

Converting perl regex to sed regex

I am having trouble parsing rpm filenames in a shell script.. I found a snippet of perl code that will perform the task but I really don't have time to rewrite the entire script in perl. I cannot for the life of me convert this code into something sed-friendly: if ($rpm =~ /(*)-(*)-(*)\.(.*)/)... (1 Reply)
Discussion started by: suntzu
1 Replies

5. Shell Programming and Scripting

Regex: Extract substring between 2 separator

Hi Input: aa-bb-cc-dd.ee.ff.gg Output: dd I want to get the word after the last '-' until the first dot I have tried with regex lookbehind and lookahead like this: (?<=-).*(?=\.) but his returns too much bb-cc-dd.ee.ff (7 Replies)
Discussion started by: chitech
7 Replies

6. Solaris

Find and sed for an email address in Solaris 10

in Solaris 10 I am able to run: find . -type f -name "copy*" exec grep example.com {} \; and I get results. but when I try to find and sed: find . -type f -name "copy*" exec sed -e 's/user@example\.com/user2@example\.com' {} \; the command executes correctly but doesn't change... (6 Replies)
Discussion started by: os2mac
6 Replies

7. Forum Support Area for Unregistered Users & Account Problems

Cant use certain email address

I tried to re-register using my new email address which is <firstname>@<surname>.me But it never sent out the email confirmation. I had to hit the back button and use my gmail address instead and it came through instantly. Is there a problem with using .me addresses? (1 Reply)
Discussion started by: frustin
1 Replies

8. Shell Programming and Scripting

Sed: -e expression #1, char 16: unterminated address regex

I am trying to grep for a particular text (Do action on cell BL330) in a text file(sample.gz) which is searched in the content filtered by date+timestamp (2016-09-14 01:09:56,796 to 2016-09-15 04:10:29,719) on a remote machine and finally write the output into a output file on a local machine. ... (23 Replies)
Discussion started by: rbadveti
23 Replies

9. UNIX for Beginners Questions & Answers

Sed: -e expression #1, char 20: unterminated address regex

I am trying to add word in last of particular line. the same command syntex is running on prompt. but in bash script give error."sed: -e expression #1, char 20: unterminated address regex" Please help. for i in `cat servername`; do ssh -q -t root@$i sed -i '/simple_allow_groups =/s/$/,... (4 Replies)
Discussion started by: yash_message
4 Replies
IO::All::FTP(3pm)					User Contributed Perl Documentation					 IO::All::FTP(3pm)

NAME
IO::All::FTP - Extends IO::All to FTP URLs SYNOPSIS
use IO::All; "hello world " > io('ftp://localhost/test/x'); # save to FTP io('ftp//example.org/pub/xyz') > io('xyz'); # GET to file # two ways of getting a file with a password: $content < io('ftp://me:secret@example.org/xyz'); $content < io('ftp://example.org/xyz')->user('me')->password('secret'); DESCRIPTION
This module extends IO::All for dealing with FTP URLs. Note that you don't need to use it explicitly, as it is autoloaded by IO::All whenever it sees something that looks like an FTP URL. METHODS
This is a subclass of IO::All::LWP. The only new method is "ftp", which can be used to create a blank IO::All::FTP object; or it can also take an FTP URL as a parameter. Note that in most cases it is simpler just to call io('ftp//example.com'), which calls the "ftp" method automatically. OPERATOR OVERLOADING
The same operators from IO::All may be used. < GETs an FTP URL; > PUTs to an FTP URL. SEE ALSO
IO::All::LWP, IO::All, LWP. AUTHORS
Ivan Tubert-Brohman <itub@cpan.org> and Brian Ingerson <ingy@cpan.org> COPYRIGHT
Copyright (c) 2007. Ivan Tubert-Brohman and Brian Ingerson. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See <http://www.perl.com/perl/misc/Artistic.html> perl v5.10.0 2007-03-29 IO::All::FTP(3pm)
All times are GMT -4. The time now is 06:33 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy