some_file.txt contains
abcd
efgh
ijkl
mnop
I want to get a specific line
I’m not saying like this though
x = File.read(‘some_file.txt’)
if x.include?(‘abcd’)
puts “abcd”
Because we are looking for ‘abcd’ in here but in my case it has to work
with any lines of a file.
(Awesome)x2 for you if this is possible without any gems anyways
post me something.
Thanks
cynic limbu wrote in post #1161026:
some_file.txt contains
abcd
efgh
ijkl
mnop
I want to get a specific line
File.readlines(‘some_file.txt’).grep(/abcd/)
Regis d’Aubarede wrote in post #1161079:
cynic limbu wrote in post #1161026:
some_file.txt contains
abcd
efgh
ijkl
mnop
I want to get a specific line
File.readlines(‘some_file.txt’).grep(/abcd/)
To be able to grep the specific content/line from a file you have to
know what string you want like in your code it’s ‘abcd’ but what I’m
asking is if we can get a line from a file without defining what string
I want.
Getting a random line would work but since it’s random we have to expect
all sort of stuff.
Here is an explanation in a language that does not exist.
x = File.read(‘some_file.txt’)
=> “abcd\nefgh\nijkl\nmnop\n”
y = get_second_line(“x”)
=> “efgh”
cynic limbu wrote in post #1161095:
x = File.read(‘some_file.txt’)
=> “abcd\nefgh\nijkl\nmnop\n”
y = get_second_line(“x”)
=> “efgh”
File.readlines(‘some_file.txt’)[1]
To get a random line you can do this:
File.readlines(‘some_file.txt’).shuffle[0]
File.readlines(‘some_file.txt’).cycle
File.readlines(‘some_file.txt’)[1]
You’re awesome. Thank you.