Solved

searching text files for email addresses

Posted on 2013-01-28
3
261 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

I recently rediscovered rails when I needed a holiday project and decided to build a management dashboard for the company where I work.  With it being a project done in my free time, I could focus my time on learning the basics rather than trying to…
Recently I spent hours debugging an issue in a Rails project where ActiveRecord was causing MySQL errors trying to create a User object of a class at the top level of a Single Table Inheritance model structure.  It turns out `.create` behaves differ…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

717 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