Click here to Skip to main content
15,891,828 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm reading from a file say - list.txt and check if strings from list.txt are present in multiple files in a directory. (E:\work...). Below is my code, its working as expected.

pool = ''
rows = Array[]
id_files = Dir.glob("E:\work'*-access.txt")

value=File.open('E:\nando\list.txt').read
value.each_line do |line|
    line.chomp!
    id_files.each do |file_name|
     text = File.read(file_name)
    new_content = text.gsub( /#{Regexp.escape(line)}\,\s/, '').gsub( /#{Regexp.escape(line)}/, '' )

    unless text == new_content

      text.each_line do |li|
        if li.match(/#{Regexp.escape(line)}/) then
         rows << li # I didn't complete the array part
          puts rows[0]
           puts rows[1]
           pool = li.split(" ")[0] 
        end
      end 
       File.open('E:\Removed_users.txt', 'a') { |log| 
       log.puts "Removed from: #{file_name}"
       log.puts "Removed id : #{line}"
       log.puts "Removed from row :#{pool}"
       log.puts "*" * 50 
       }
      File.open(file_name, "w") { |file| file.puts new_content }
     end
    end
  end


I'm trying to enhance the code to print in the below format to a log file. Eg., for strings - class\234ha and cap\7y6t5 from one of the file in the directory.

Removed class\234ha from row = id and dept
Removed cap\7y6t5 from row = id


E:\work\sample-access.txt
CDA created on September 20th 1999
 Owner: Edward Jenner
 Access IDs,
 id = class\234ha, class\poi23, class\opiuj, cap\7y6t5
 dept = sub\6985de, ret\oiu87, class\234ha
 rec = ret\oiu87


I'm able to make it print only for a single time.i.e. for class\234ha I'm getting output as

Removed from: E:\work\sample-access.txt
 Removed user : class\234ha
 Removed from row = id


ideally it should be

Removed from: E:\work\sample-access.txt
Removed user : class\234ha
Removed from row = id and dept


any suggestions would be really helpful. Thanks.

What I have tried:

pool = ''
rows = Array[]
id_files = Dir.glob("E:\work'*-access.txt")

value=File.open('E:\nando\list.txt').read
value.each_line do |line|
    line.chomp!
    id_files.each do |file_name|
     text = File.read(file_name)
    new_content = text.gsub( /#{Regexp.escape(line)}\,\s/, '').gsub( /#{Regexp.escape(line)}/, '' )

    unless text == new_content

      text.each_line do |li|
        if li.match(/#{Regexp.escape(line)}/) then
         rows << li # I didn't complete the array part
          puts rows[0]
           puts rows[1]
           pool = li.split(" ")[0] 
        end
      end 
       File.open('E:\Removed_users.txt', 'a') { |log| 
       log.puts "Removed from: #{file_name}"
       log.puts "Removed id : #{line}"
       log.puts "Removed from row :#{pool}"
       log.puts "*" * 50 
       }
      File.open(file_name, "w") { |file| file.puts new_content }
     end
    end
  end
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900