HELP simple script to find e-mail address on a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting HELP simple script to find e-mail address on a file
# 1  
Old 07-30-2014
HELP simple script to find e-mail address on a file

Hello guys, im new to to unix/linux

i have a text file like this:

person1@test.com iisiiasasas
person2@test.com 123w2 3233
sajsja person3@test.com jsajjsa
sajsjasaj person4@test.com


I want to extract only e-mail address and get rid of all other stuff, i want an output like this

person1@test.com
person2@test.com
person3@test.com
person4@test.com

Can someone help? thanks for your time
# 2  
Old 07-30-2014
Please use code tags as required by forum rules!
Try
Code:
grep -o "[^ ]*@[^ ]*" file
person1@test.com
person2@test.com
person3@test.com
person4@test.com

# 3  
Old 07-30-2014
Quote:
Originally Posted by RudiC
Please use code tags as required by forum rules!
Try
Code:
grep -o "[^ ]*@[^ ]*" file
person1@test.com
person2@test.com
person3@test.com
person4@test.com



Thanks but that didnt work on my enviroment.. what could be wrong?
# 4  
Old 07-30-2014
What IS your environment? You failed to mention it. What IS not working? Check the grep man page to see if your grep supports the -o option.
# 5  
Old 07-30-2014
Not highly robust, but for your sample input, here are a couple of ways to do it using awk in the 1st case, and just using standard shell built-in features in the 2nd case:
Code:
echo 'Using awk:'
awk '{for(i = 1; i <= NF; i++) if($i ~ /@/) print $i}' file
printf '\nJust using shell built-ins:\n'
while read -r line
do	set -- $line
	while [ $# -gt 0 ]
	do	if [ "${1#*@}" != "$1" ]
		then	printf '%s\n' "$1"
		fi
		shift
	done
done < file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to find available IP address

Hi, I am trying to write a script that will check all used IP on the server and then print me an addressees that are not in use. Problem is in comparing two variables #!/bin/bash NETSTAT=$(netstat -ntp | awk '{ print $4 }' | grep -v "127.0.0.1" | cut -d ":" -f1 | grep "^"|sort | uniq )... (6 Replies)
Discussion started by: nemesis911
6 Replies

2. UNIX for Dummies Questions & Answers

Finding e-mail address using "find" command

any useful command to find and e-mail address hardcoded into a cfg file on an specific server? I already tried with:: find . -type f | xargs grep -l "nobody@foundstone.com" Thanks!! (1 Reply)
Discussion started by: JLo5621
1 Replies

3. Shell Programming and Scripting

mail using mail address in a file

I have a file which contains few email address. I have few scripts which use the same mail address. so if any change in mail id like if any user do not want to receive the mail i can just edit a single file instead of many scripts. So i want the scripts to use that file. How can this be done. (2 Replies)
Discussion started by: gpk_newbie
2 Replies

4. Shell Programming and Scripting

mailx -s not sending the file to mail address

Hi All, OS:Red Hat Linux 4 86x64 Below is my shell script which is not sending mail to the mail recipient: #!/bin/bash export MAILLIST="xyz@yahoo.com" cd <path_to_the_script_perf_report.sql> sqlplus / as sysdba @perf_report.sql if then cat <path_to_the_script/*MONTHLY*REPORT*.lst... (6 Replies)
Discussion started by: a1_win
6 Replies

5. Shell Programming and Scripting

UNIX Script to query Active Directory: give cn (NT login name) and receive mail (Email address)

Hi folks I need to write UNIX script (with ldapsearch) to query Active Directory. Input is NT login name and output is Email address. Attached a screenshot of Sysinternals "AD Explorer". I need to do the same in CLI. http://i.imgur.com/4s6FB.png I am absolute LDAP/ldapsearch noob. (0 Replies)
Discussion started by: slashdotweenie
0 Replies

6. Shell Programming and Scripting

Perl how to replace e-mail address from the file

I have a script which updates the users e-mail address according to wherever the users type in the browser. The script does other stuffs but this what i am struggling with ..lol. Basically, we are using the command below to try to update the e-mail, however since the e-mail address has "@" the perl... (2 Replies)
Discussion started by: cacm1975
2 Replies

7. Shell Programming and Scripting

Simple Find file Script.....

Im trying to make a very simple find the first file with the .zip extension in a specific folder and open that file. The folder path and file name will vary every-time and it may contain spaces. If I try to look For this example the folder directory is /Users/username/Desktop/testfolder/abc... (6 Replies)
Discussion started by: elbombillo
6 Replies

8. UNIX for Dummies Questions & Answers

send mail file from server to another address

HP-UX B11.23 ia64 I have a users mail inbox in /var/mail I want to send all the mail there to another address (an Exchange address). At the Exchange address, I want it to appear as the original separate emails, with attachments in their original form (e.g. still MIME encoded). Is that... (6 Replies)
Discussion started by: LisaS
6 Replies

9. Shell Programming and Scripting

Pull E-mail address from file, send e-mail

Hello, I am new to perl and need to create a script that will read a file and pull a name from the file and send e-mail. How can I use the following awk statement in a perl script? grep UNIXadmins /root/mail.conf | awk '{ print $2}' and use the output to send a e-mail. Any help would... (1 Reply)
Discussion started by: DC Heard
1 Replies

10. Shell Programming and Scripting

script to find a file and send a mail

I need a shell script which checks for a file in a particuler folder and should send me a mail if the file of that name is present. Please help me on this.I am new to shell scripting. (6 Replies)
Discussion started by: jayaramanit
6 Replies
Login or Register to Ask a Question