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
Solved

searching text files for email addresses

Posted on 2013-01-28
3
239 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
  • 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

856 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