Solved

searching text files for email addresses

Posted on 2013-01-28
3
247 Views
Last Modified: 2013-02-06
Hello
Want to search all txt files in a directory and return only the file names and lines in those files where a line contains an email address.
Regards
0
Comment
Question by:PHIL Sawyer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 

Author Comment

by:PHIL Sawyer
ID: 38827171
This is my attempt so far - don't want to have to enter all files in "file_lst  manually

# fill in the path of the input files here
my_path = "Z:/"
# put the local filenames of theinput files here
file_lst = ["FILE1.doc","FILE2.doc,etc,etc]

sink = File.open(my_path + "A_JOINED.txt", "w")
file_lst.each do |fl|
  # open the file and access each single line
  File.open(my_path + fl, "r").each do |line|
        if line =~/emailaddresses/i then
    sink.write line
   end
end
end
0
 
LVL 10

Accepted Solution

by:
Andrew Doades earned 500 total points
ID: 38847238
Dir.chdir("C:/EE/") do
    Dir["*.txt"].each do |tempfile|
		f = File.open(tempfile)
		content = f.read
		email_format = Regexp.new(/\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}\b/)
		emails = content.scan(email_format).uniq
		puts emails
	end
end

Open in new window

0
 

Author Closing Comment

by:PHIL Sawyer
ID: 38859032
Thanks
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
customize rails URL 6 503
Issue installing Ruby 1.9.2 on CentOS 5.8 6 1,309
Need to extract string via Regex 4 337
Help with Redmine installation on Ubuntu 14.04 6 328
Article by: narshlob
If you've ever programmed in Ruby and have come across either a proc or a lambda, you might have been wondering what the difference is between the two and when you would use one over the other. This article will try to explain the difference between…
In Ruby, Call or invoke a API DLL library is easily via Win32API class, win32-api gem or other gems. For general DLL API call, there are quite a few references, some good tips list below: http://www.rubytips.org/2008/05/13/accessing-windows-api-fro…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question