?
Solved

searching text files for email addresses

Posted on 2013-01-28
3
Medium Priority
?
295 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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

650 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