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
-1
A
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
- 1. Auf der Suche nach einem bestimmten Designmuster
- 2. Suche nach bestimmten Argumenten in Text
- 3. Suche nach bestimmten Pfad in Neo4j schnell
- 4. Suche nach bestimmten Wörtern in einem Satz
- 5. der Suche nach einem bestimmten Element in Standardwörterbuch python
- 6. In der Tat API Suche nach bestimmten Unternehmen
- 7. Suche nach XPath unter einem bestimmten Elemente
- 8. Suche nach divs mit einer bestimmten Breite
- 9. Suche nach bestimmten Daten aus einer Datei
- 10. auf der Suche nach bestimmten WYSIWYG-Editor-Funktionalität. Empfehlungen?
- 11. jQuery - Suche nach bestimmten Element vor der Codezeile
- 12. Suche nach der nächstgrößeren Ganzzahl
- 13. Suche nach bestimmten Zeichenfolge in einem Feld in awk
- 14. Suche nach Tweets in einem bestimmten Zeitfenster in Python
- 15. Suche nach einer Datei in einem bestimmten Verzeichnis C#
- 16. Suche nach einem bestimmten Wort in einer Datenbanktabelle
- 17. Suche nach einer bestimmten Zelle in Google Sheet
- 18. Suche nach einem bestimmten gesprochenen Wort in Audiodateien
- 19. Suche nach einer bestimmten ID mit Rest in Grails
- 20. Suche in einem bestimmten Dokument nach Erhalt seiner docId
- 21. Suche nach einer bestimmten Zeichenfolge in Oracle Clob-Spalte
- 22. Suche nach einem bestimmten JToken namentlich in einer JObject Hierarchie
- 23. preg_match in Javascript und die Suche nach bestimmten Variablen
- 24. Eclipse, Suche nach String innerhalb der Methode
- 25. DataTables Suche in bestimmten Spalten
- 26. Suche nach der Anzahl der XML-Dateien mit einem bestimmten Wort in einem Verzeichnis
- 27. MATLAB: Suche nach der Zeilennummer eines bestimmten Wertes in einem großen Vektor mit der Funktion find
- 28. Java-Code - auf der Suche nach Quellcode
- 29. Bildschirm nach der Suche vorladen?
- 30. Auf der Suche nach Anleitung
Natürlich ist das möglich, aber was hast du bisher versucht? – NaN
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. –