Sponsored Content
Top Forums Shell Programming and Scripting SED With Regex to extract Email Address Post 302618829 by ragz_82 on Wednesday 4th of April 2012 01:18:19 PM
Old 04-04-2012
SED With Regex to extract Email Address

Hi Folks,
In my program, I have a variable which consists of multiple lines. i need to use each line as an input. My intention is to extract the email address of the user in each line and use it to process further.

The email address could be anywhere in the whole line. But there will be only one and I need to extract it. The most complex possible format of the email ID is:
first-name.last-name@xyz.com

In other words, first and last names are separated by a period (.) and the first and last names may have a hyphen (-). The domain name (@xyz.com) is fixed and only has letters, no numbers.

We have a Solaris OS and I am using Korn shell. I read some examples on SED command and have made a few attempts to use it. But every time, no matter what regex I use, I get the entire input as the output. I must be doing something very basic thing wrong. Could you please suggest?

Code:
$ echo '92' | sed '/[0-9]+/p'
92

$ echo 'email92' | sed '/[0-9]+/p'
email92

$ echo "abc.xyz@comp.com" | sed '/\([A-Za-z0-9]+\)\(-*\)\([A-Za-z0-9]*\)\(\.\)\([A-Za-z0-9]+\)\(-*\)\([A-Za-z0-9]*\)@comp.com/p'
abc.xyz@comp.com

$ echo "Email address is abc.xyz@comp.com" | sed '/\([A-Za-z0-9]+\)\(-*\)\([A-Za-z0-9]*\)\(\.\)\([A-Za-z0-9]+\)\(-*\)\([A-Za-z0-9]*\)@comp.com/p'
Email address is abc.xyz@comp.com

Note that below is the regex that I arrived at to extract the email address.
Code:
/\([A-Za-z0-9]+\)\(-*\)\([A-Za-z0-9]*\)\(\.\)\([A-Za-z0-9]+\)\(-*\)\([A-Za-z0-9]*\)@comp.com

Any help is greatly appreciated.

Moderator's Comments:
Mod Comment Please use code tags. Video tutorial on how to use them

Last edited by Scrutinizer; 04-04-2012 at 05:05 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
Log::Dispatch::Email(3pm)				User Contributed Perl Documentation				 Log::Dispatch::Email(3pm)

NAME
Log::Dispatch::Email - Base class for objects that send log messages via email VERSION
version 2.32 SYNOPSIS
package Log::Dispatch::Email::MySender; use Log::Dispatch::Email; use base qw( Log::Dispatch::Email ); sub send_email { my $self = shift; my %p = @_; # Send email somehow. Message is in $p{message} } DESCRIPTION
This module should be used as a base class to implement Log::Dispatch::* objects that send their log messages via email. Implementing a subclass simply requires the code shown in the SYNOPSIS with a real implementation of the "send_email()" method. CONSTRUCTOR
The constructor takes the following parameters in addition to the standard parameters documented in Log::Dispatch::Output: o subject ($) The subject of the email messages which are sent. Defaults to "$0: log email" o to ($ or @) Either a string or a list reference of strings containing email addresses. Required. o from ($) A string containing an email address. This is optional and may not work with all mail sending methods. o buffered (0 or 1) This determines whether the object sends one email per message it is given or whether it stores them up and sends them all at once. The default is to buffer messages. METHODS
o send_email(%p) This is the method that must be subclassed. For now the only parameter in the hash is 'message'. o flush If the object is buffered, then this method will call the "send_email()" method to send the contents of the buffer and then clear the buffer. o DESTROY On destruction, the object will call "flush()" to send any pending email. AUTHOR
Dave Rolsky <autarch@urth.org> COPYRIGHT AND LICENSE
This software is Copyright (c) 2011 by Dave Rolsky. This is free software, licensed under: The Artistic License 2.0 (GPL Compatible) perl v5.14.2 2012-05-25 Log::Dispatch::Email(3pm)
All times are GMT -4. The time now is 05:28 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy