简体   繁体   中英

Print the first word of the line of matched string in Ruby

My requirement is to check if a given string (reading it from a text file) exists in any of the files in a particular folder, if so store and print the first word of the line of the matched string.

Below is code snippet,

.......
 .......
    my_files.each do |file_name|
      puts "File Name: #{file_name}"
      content = File.read(file_name)
      changed = content.gsub( /#{Regexp.escape(id_value)}/, '' ) #'id_value' is from the first level loop ,stores string value(for every iteration).

      if content.include?("#{id_value}")
           print "its there\n"
           Row = content.split.first
           puts "From: #{Row}"
        end 

One of the files in the folders

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

say if the id_value is class\\234ha

for first iteration, it should give the output as 'id' and 'dept' but the output is 'CDA'. Also I'm facing the below warning too.

test.rb:19: warning: already initialized constant Row test.rb:19: warning: previous definition of Row was here From: class\\poi23

Any suggestions please. I have looked for other options too but none worked.Beginner to ruby so kindly excuse my ignorance.Thanks.

If there is anything like this:

File.open('sample.txt').grep(/class\\234ha/) { |line| line.split.first } => ["id", "dept"]

Do pass a block to the grep method

Here is an example from a script that I had that does what you are looking for. It's fairly strait forward if you use the each_line method of a file object.

#!/usr/bin/env ruby
regex_to_find = Regexp.new Regexp.escape(ARGV[0])
files = Dir.glob ARGV[1]
files.each do |f|
  current_file = File.new f
  current_file.each_line do |l|
    if l =~ regex_to_find
      puts "#{f} #{current_file.lineno}: first word = #{l.split.first}, full line: #{l}"
    end
  end
end

If you run this script on a directory with a file containing the data you show above. you get the following output. Which is I think what you are looking for.

$ ./q43950329.rb  'class\234ha' "*"
q43950329_data 4: first word = id, full line: id = class\234ha, class\poi23, class\opiuj, cap\7y6t5
q43950329_data 5: first word = dept, full line: dept = sub\6985de, ret\oiu87, class\234ha

Note the above output is in a file called q43950329.rb and the following file exists in the current directory called q43950329_data

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM