2016-07-08 8 views
-1

Ich versuche, ein Ruby-Tool zu erstellen, das durch eine Datei sucht nach einer bestimmten Zeichenfolge, und wenn es dieses Wort findet, speichert es es in einer Variablen. Wenn nicht, wird "Wort nicht gefunden" auf der Konsole ausgegeben. Ist das möglich? Wie kann ich das programmieren?Suche nach bestimmten Ruby-Wort in der txt-Datei

+0

Natürlich ist das möglich, aber was hast du bisher versucht? – NaN

+0

Zuerst habe ich File.read verwendet, um die Datei zu lesen, aber ich weiß nicht, wie ich die Datei durchsuche und durch ein bestimmtes Schlüsselwort suche. –

Antwort

0

Sie können File#open Methode und readlines Methode wie folgt verwenden.

test.txt

This is a test string. 
Lorem imsum. 
Nope. 

Code

def get_string_from_file(string, file_path) 
    File.open(file_path) do |f| 
    f.readlines.each { |line| return string if line.include?(string) } 
    end 
    nil 
end 

file_path = './test.txt' 
var = get_string_from_file('Lorem', file_path) 
puts var || "word not found" 
# => "Lorem" 

var = get_string_from_file('lorem', file_path) 
puts var || "word not found" 
# => "word not found" 

Ich hoffe, das heps.

0

Hier einige Beispiele, wie Sie ein Stichwort in einer Textdatei finden konnten IO vom Kern Rubin mit: http://ruby-doc.org/core-2.3.1/

In find_word_in_text_file.rb:

# SETUP 
# 
filename1 = 'file1.txt' 
filename2 = 'file2.txt' 

body1 = <<~EOS 
    PHRASES 

    beside the point 
    irrelevant. 

    case in point 
    an instance or example that illustrates what is being discussed: the “green revolution” in agriculture is a good case in point. 

    get the point 
    understand or accept the validity of someone's idea or argument: I get the point about not sending rejections. 

    make one's point 
    put across a proposition clearly and convincingly. 

    make a point of 
    make a special and noticeable effort to do (a specified thing): she made a point of taking a walk each day. 
EOS 

body2 = <<~EOS 
    nothing to see here 
    or here 
    or here 
EOS 

# write body to file 
File.open(filename1, 'w+') {|f| f.write(body1)} 

# write file without matching word 
File.open(filename2, 'w+') {|f| f.write(body2)} 

# METHODS 
# 
# 1) search entire file as one string 
def file_as_string_rx(filename, string) 

    # http://ruby-doc.org/core-2.3.1/Regexp.html#method-c-escape 
    # http://ruby-doc.org/core-2.3.1/Regexp.html#method-c-new 
    rx = Regexp.new(Regexp.escape(string), true) # => /whatevs/i 

    # read entire file to string 
    # http://ruby-doc.org/core-2.3.1/IO.html#method-i-read 
    text = IO.read(filename) 

    # search entire file for string; return first match 
    found_word = text[rx] 

    # print word or default string 
    puts found_word || "word not found" 
    # —OR— 
    #STDOUT.write found_word || "word not found" 
    #STDOUT.write "\n" 
end 

# 2) search line by line 
def line_by_line_rx(filename, string) 

    # http://ruby-doc.org/core-2.3.1/Regexp.html#method-c-escape 
    # http://ruby-doc.org/core-2.3.1/Regexp.html#method-c-new 
    rx = Regexp.new(Regexp.escape(string), true) # => /whatevs/i 

    # create array to store line numbers of matches 
    matches_array = [] 

    # search each line for string 
    # http://ruby-doc.org/core-2.3.1/IO.html#method-c-readlines 
    #lines = IO.readlines(filename) 
    # 
    # http://ruby-doc.org/core-2.3.1/Enumerable.html#method-i-each_with_index 
    # http://stackoverflow.com/a/5546681/1076207 
    # "Be wary of "slurping" files. That's when you 
    # read the entire file into memory at once. 
    # The problem is that it doesn't scale well. 
    #lines.each_with_index do |line,i| 
    # 
    # —OR— 
    # 
    # http://ruby-doc.org/core-2.3.1/IO.html#method-c-foreach 
    i = 1 
    IO.foreach(filename) do |line| 

    # add line number if match found within line 
    matches_array.push(i) if line[rx] 

    i += 1 
    end 

    # print array or default string 
    puts matches_array.any? ? matches_array.inspect : "word not found" 
    # —OR— 
    #STDOUT.write matches_array.any? ? matches_array.inspect : "word not found" 
    #STDOUT.write "\n" 
end 

# RUNNER 
# 
string = "point" 

puts "file_as_string_rx(#{filename1.inspect}, #{string.inspect})" 
file_as_string_rx(filename1, string) 

puts "\nfile_as_string_rx(#{filename2.inspect}, #{string.inspect})" 
file_as_string_rx(filename2, string) 

puts "\nline_by_line_rx(#{filename1.inspect}, #{string.inspect})" 
line_by_line_rx(filename1, string) 

puts "\nline_by_line_rx(#{filename2.inspect}, #{string.inspect})" 
line_by_line_rx(filename2, string) 

# CLEANUP 
# 
File.delete(filename1) 
File.delete(filename2) 

Befehlszeilen:

 
$ ruby find_word_in_text_file.rb 
file_as_string_rx("file1.txt", "point") 
point 

file_as_string_rx("file2.txt", "point") 
word not found 

line_by_line_rx("file1.txt", "point") 
[3, 6, 7, 9, 10, 12, 15, 16] 

line_by_line_rx("file2.txt", "point") 
word not found 
Verwandte Themen