Nothing To Lose

If you don’t have it, how can you lose it!
Subscribe
  • Home
  • Contact Me
  • Sitemap
  • About
  • FAQ
  • My Tag Cloud

Catching email id’s from file(s) using grep and other utils

April 18, 2009 By: Dexter Category: BASH, Linux Commands, Regular Expressions, Shell Scripting, Tutorial

Here is a simple mechanism that you can use to collect all the email id(s) from a file(s) into a single file. To do this we will be using the following command cat , grep, sort and uniq.

This one liner should do the work

cat file | grep -io ‘\<[^-.][0-9A-Za-z\.\-\_]\+@[0-9A-Za-z.]\+\>‘ | sort | uniq

If you want all the id’s in some file then redirect the above command to a file.

cat file | grep -io ‘\<[^-.][0-9A-Za-z\.\-\_]\+@[0-9A-Za-z.]\+\>‘ | sort | uniq > mailid.txt

Now lets convert this into a shell script where we shall accept a directory name from the user. This directory will be the one containing the files having the email ids
You can download the script from here Script to retrieve Email ids form files in a directory
I have noticed the copy paste of the code below is not working because of formatting characters.

#!/bin/bash
clear
echo -n “Enter the name of a DIRECTORY from where you want to pick up email id’s: “;
read dirname;

# check if the entered name is a directory

if [ -d $dirname ];then

cd $dirname; #  if it exists change to the directory

else

echo “+============================+”

echo “| Check your directory name! |”

echo “+============================+”

exit 1;

fi

# Loop through all file  in the given directory

for files in *

do

if [ ! -d $files ];then

# process all files and store them in a temporary file in users home dir

echo “Processing file $files”;

cat $files | egrep -io ‘\<[^-.][0-9A-Za-z\.\-\_]+@[0-9A-Za-z.]+\>‘ >> ~/$$;

echo “Processed”;

fi

done

cd -  # get back to previous working dir, i am assuming it was home

# sort the emails ids in the file, remove duplicates and store in a final file.

sort ~/$$ | uniq >> emailids.$$

# remove the temporary file

rm ~/$$

# tell the user where the mail ids are stored

echo

echo “+=================================================+”

echo ” Your email ids are available in ~/emailids.$$ ”

echo “+=================================================+”

exit 0;

Well I should warn you, the regular expression will catch anything that looks like an email id, so you might end up having lots of things that looks like an email id.
[end]

Share This Article
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • blogmarks
  • description
  • E-mail this story to a friend!
  • LinkedIn
  • MySpace
  • Reddit
  • Slashdot
  • StumbleUpon
  • Technorati
  • TwitThis
Tags: Add new tag, BASH, grep, Regex, Shell Scripting, sort, uniq

Comments are closed.

← Jigsaw Sphere With Text
Setting date on Linux through the command line →
  • Recent Posts

    • Making a circle in GIMP
    • Sony Ericsson W350i on Linux (Mandriva 2009.1)
    • Shifting to Window Maker
    • Shell Conversation.
    • PLUG at CMDA IT Expo 2009
  • Categories

    • BASH (3)
    • Blog (2)
    • Camp (1)
    • Foss (4)
    • Fun (7)
    • gnu (1)
    • Hacks (2)
      • repair (1)
    • Internet (1)
    • Linux (7)
    • Linux Commands (2)
    • Photos (2)
    • Projects (2)
      • DIY (1)
      • electronic (1)
    • Random Thoughts (2)
    • Repairs (1)
    • Review (3)
    • Shell Scripting (1)
    • tools (3)
    • Tutorial (24)
      • GIMP (16)
      • PHP (2)
      • Regular Expressions (6)
    • Uncategorized (1)
    • Web Development (2)
    • Web Graphics (3)
    • Wordpress (3)
  • Monthly Blips

    April 2009
    M T W T F S S
    « Mar   May »
     12345
    6789101112
    13141516171819
    20212223242526
    27282930  
  • Archives

    • March 2010 (1)
    • January 2010 (3)
    • December 2009 (1)
    • August 2009 (3)
    • July 2009 (2)
    • June 2009 (4)
    • May 2009 (1)
    • April 2009 (5)
    • March 2009 (6)
    • February 2009 (1)
    • January 2009 (8)
    • December 2008 (10)
  • What Others Thought

    • SHANE on Sony Ericsson W350i on Linux (Mandriva 2009.1)
    • JULIAN on Making a circle in GIMP
    • WILLIE on Shell Conversation.
    • KEVIN on PLUG at CMDA IT Expo 2009
    • DAN on Shell Conversation.
  • Search

  • Tags

    Add new tag BASH Blog blogging Brain Buttons Camp CD/DVD label Data Card Date Email Emptiness event free software Fun GIMP gnu grep Hacks Huawei ec325 infrared reciever Linux Linux Commands LIRC Mandriva must haves Photos PHP Regex reliance netconnect repair screw driver Shell Scripting sort Sphere Sticker t-shirt tools Tutorial Tutorials uniq Wallpaper wc Web Graphics Wordpress
  • RSS Gaurav's Blog

    • How Much! July 8, 2010 gaurav
    • Damaged silencer January 28, 2010 gaurav
    • Interesting Plumbing January 7, 2010 gaurav
    • Apple – and how to sell it costly December 26, 2009 gaurav
    • Punjabi Dhaba South Indian Menu December 3, 2009 gaurav
    • Very Clear instructions. November 22, 2009 gaurav
    • Proprietary Meditation September 16, 2009 gaurav
    • Please Clean You Car. September 10, 2009 gaurav
    • Riding Solo To Top Of The World. September 7, 2009 gaurav
    • Toward’s the end of the rainbow August 15, 2009 gaurav
  • Blogroll

    • Aditya ( Darth Chatri )
    • Home Page
    • Koolhead17
    • Mukund J
    • Niyam
    • Other blog
    • Shantnoo ( aka Dhoomketu )
    • Sudhanwa
    • Vinay Pawar (aka Zoyd)
  • I Support

    • FireFox
    • GIMP
    • OpenOffice
  • Profiles

    • Linkedin
    • Linux Counter
  • Wordpress Related

    • Themes
    • Wordpress Help


Nothing To Lose © 2007 - 2010 All Rights Reserved. Using WordPress 2.6.5 Engine
Entries and Comments.

Prosumer ver. 1.9 design by WP GPL