Link to home
Start Free TrialLog in
Avatar of PHIL Sawyer
PHIL SawyerFlag for United Kingdom of Great Britain and Northern Ireland

asked on

searching text files for email addresses

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
Avatar of PHIL Sawyer
PHIL Sawyer
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Andrew Doades
Andrew Doades
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks