Ruby: Append text to the 2nd line of a file - ruby

The Ruby script i am writing is going to be run every morning and will pull information about backup files and write them to a csv file. This file has column names on the first line.
I have gotten it to work by appending to the end of the file:
open("#{curDir}/Backup_Times.csv", 'a') do |f|
...
end
I would like to see the newest data first without having to sort in Excel.
In Ruby, is there a way to write this data starting at the 2nd line of the file?

You write a new file, applying your change at the desired line, then rename result back to the original file name. The following method will copy the file and yield the output file object to a block at the correct line, so that block can output your new lines.
def insert_lines_following_line file, line_no
tmp_fn = "#{file}.tmp"
File.open( tmp_fn, 'w' ) do |outf|
line_ct = 0
IO.foreach(file) do |line|
outf.print line
yield(outf) if line_no == (line_ct += 1)
end
end
File.rename tmp_fn, file
end
insert_lines_following_line( "#{curDir}/Backup_Times.csv", 1 ) do |outf|
# output new csv lines in this block
outf.puts ['foo','bar','baz',1,2,3].join(",") # or however you build your csv line
end

Related

Is there a way to delete a line in a text file if it matches a specific text?

My text file contains an email/password list to set up accounts. Once I've used an email and password combo I would like to erase it form the text file.
My text file looks like this:
email,pass
email,pass
etc..
once I've used the e/p combo I would like to delete it from the file:
File.open("yahoo_accounts.txt") do |email|
email.each do |item|
email, password = item.chomp.split(',')
emails << email
passwords << password
emails.zip(passwords) { |name, pass|
browser = Watir::Browser.new :ff
#using the email and pass
File.open("yahoo_accounts.txt", "w") do |out_file|
File.foreach("yahoo_accounts.txt","r") do |line|
out_file.puts line unless line == '#{name},#{pass}'
end
end
browser.close
end
end
The problem occurs when I try to delete them from the file. I get an "browser.rb:382:in `assert_exists': browser was closed (Watir::Exception::Error)",
but that might just be the browser closing.
If all the e/p's are extracted (meaning there is nothing to delete) in the beginning, how can I loop it to keep going, instead of ending in error after the first zip loop?
If the file is not huge, you can read it into an array, modify the array and then write the array to the file, overwriting the previous contents.
Code
def remove_lines(fname, user_name, password)
IO.write(fname, IO.read(fname).gsub(/^#{user_name},#{password}\n/, ''))
end
Example
Let's create some data:
text =<<_
Bubba,boar
Henrietta,vespa
Luigi,pink
Bubba,boar
Luigi,mauve
_
#=> "Bubba,boar\nHenrietta,vespa\nLuigi,pink\nBubba,boar\nLuigi,mauve\n"
and write it to a file:
FName = "tmp"
IO.write(FName, text)
#=> 61
We can confirm what we've written to file:
IO.read(FName)
#=> "Bubba,boar\nHenrietta,vespa\nLuigi,pink\nBubba,boar\nLuigi,mauve\n"
Now remove the lines for user_name/password #=> Bubba/boar:
remove_lines(FName, "Bubba", "boar")
#=> 39
Let's confirm it worked by examining FName:
IO.read(FName)
#=> "Henrietta,vespa\nLuigi,pink\nLuigi,mauve\n"
Postscript
If something goes wrong while writing the array to file, you may lose both the old file and the new file. For that reason, it is often good practice to write the array to a temporary file, then delete the original file, then rename the temporary file to the name of the original file.
You can't write and read to the same file at the same time like that; the first line you write, will clobber the rest of the file so you won't be able to read it anymore.
You should write the changes to a temporary file as you read the lines, then move the temp file to the original file at the end. Something like:
require 'tempfile'
require 'fileutils'
temp_file = Tempfile.new('foo')
begin
File.open("yahoo_accounts.txt", 'r') do |file|
file.each_line do |line|
temp_file.puts line unless line == '#{name},#{pass}'
end
end
temp_file.close
FileUtils.mv(temp_file.path, "yahoo_accounts.txt")
ensure
temp_file.close
temp_file.unlink
end

Ruby: overwrite line in file

I am trying to log some state to a file using standard file i/o in ruby. The file will just have one line with a number in it. I want to read in the line and then on each iteration of a loop I want to update this number.
I know I can read in the file with
file = File.open('out.log', 'a+')
num = file.readline
The problem is, I don't know how to then overwrite the first line in a loop without just re-opening the file every iteration i.e.
file = File.open('out.log', 'w')
which will create an empty file
No need to open it each time:
file = File.open('out.log', File::RDWR)
10.times do |i|
file.seek(0) # rewind to the beginning of the file (line in your case)
file.write("iteration #{i}") # write what you want
# the following is just in order to show what was written
file.seek(0)
puts file.readline
end
file.close
You can use IO::open with block to close the file, when block exits automatically.
File.open("#{__dir__}/test.txt", File::RDWR) do |file|
10.times do |i|
file.rewind
file.puts("iteration #{i}")
end
end
puts File.read("#{__dir__}/test.txt")
# >> iteration 9

Printing lines from a file in ruby

When I start printing lines from a file, I get this error
#<File:0x007ff65ee297b0>
Here is the code
require 'rubygems'
File.open("sample.txt", 'r') do |f|
puts f
end
You are printing the file object. To get the contents line by line, you can use File.foreach
File.foreach('sample.txt', 'r') do |line|
puts line # called for every line
end
To process the whole file at once, you can use the read method on the file object:
File.open('sample.txt', 'r') do |file|
puts file.read # called only once
end
This is not an error. It prints correctly one line which is your File object.
Here your create a file object and you did not ask it to fetch lines or anything else for that matter.
Several good answers already. But here is another way to do it with minimal change to your code:
File.open("sample.txt", 'r').each_line do |f|
puts f
end
Another way :
IO.foreach("sample.txt") {|line| line }
Or
File.foreach('sample.txt') {|line| line }
File::open returns file handle (which apparently is being printed out as #<File:0x007ff65ee297b0>.) If you need the file content line by line you might want to use IO::readlines:
IO.readlines("sample.txt").each do |line|
puts line
end

Read Certain Lines from File

Hi just getting into Ruby, and I am trying to learn some basic file reading commands, and I haven't found any solid sources yet.
I am trying to go through certain lines from that file, til the end of the file.
So in the file where it says FILE_SOURCES I want to read all the sources til end of file, and place them in a file.
I found printing the whole file, and replacing words in the file, but I just want to read certain parts in the file.
Usually you follow a pattern like this if you're trying to extract a section from a file that's delimited somehow:
open(filename) do |f|
state = nil
while (line = f.gets)
case (state)
when nil
# Look for the line beginning with "FILE_SOURCES"
if (line.match(/^FILE_SOURCES/))
state = :sources
end
when :sources
# Stop printing if you hit something starting with "END"
if (line.match(/^END/))
state = nil
else
print line
end
end
end
end
You can change from one state to another depending on what part of the file you're in.
I would do it like this (assuming you can read the entire file into memory):
source_lines = IO.readlines('source_file.txt')
start_line = source_lines.index{ |line| line =~ /SOURCE_LINE/ } + 1
File.open( 'other_file.txt', 'w' ) do |f|
f << source_lines[ start_line..-1 ].join( "\n" )
end
Relevant methods:
IO.readlines to read the lines into an array
Array#index to find the index of the first line matching a regular expression
File.open to create a new file on disk (and automatically close it when done)
Array#[] to get the subset of lines from the index to the end
If you can't read the entire file into memory, then I'd do a simpler variation on #tadman's state-based one:
started = false
File.open( 'other_file.txt', 'w' ) do |output|
IO.foreach( 'source_file.txt' ) do |line|
if started then
output << line
elsif line =~ /FILE_SOURCES/
started = true
end
end
end
Welcome to Ruby!
File.open("file_to_read.txt", "r") {|f|
line = f.gets
until line.include?("FILE_SOURCES")
line = f.gets
end
File.open("file_to_write.txt", "w") {|new_file|
f.each_line {|line|
new_file.puts(line)
}
new_file.close
}
f.close
}
IO functions have no idea what "lines" in a file are. There's no straightforward way to skip to a certain line in a file, you'll have to read it all and ignore the lines you don't need.

Ruby: How to replace text in a file?

The following code is a line in an xml file:
<appId>455360226</appId>
How can I replace the number between the 2 tags with another number using ruby?
There is no possibility to modify a file content in one step (at least none I know, when the file size would change).
You have to read the file and store the modified text in another file.
replace="100"
infile = "xmlfile_in"
outfile = "xmlfile_out"
File.open(outfile, 'w') do |out|
out << File.open(infile).read.gsub(/<appId>\d+<\/appId>/, "<appId>#{replace}</appId>")
end
Or you read the file content to memory and afterwords you overwrite the file with the modified content:
replace="100"
filename = "xmlfile_in"
outdata = File.read(filename).gsub(/<appId>\d+<\/appId>/, "<appId>#{replace}</appId>")
File.open(filename, 'w') do |out|
out << outdata
end
(Hope it works, the code is not tested)
You can do it in one line like this:
IO.write(filepath, File.open(filepath) {|f| f.read.gsub(//<appId>\d+<\/appId>/, "<appId>42</appId>"/)})
IO.write truncates the given file by default, so if you read the text first, perform the regex String.gsub and return the resulting string using File.open in block mode, it will replace the file's content in one fell swoop.
I like the way this reads, but it can be written in multiple lines too of course:
IO.write(filepath, File.open(filepath) do |f|
f.read.gsub(//<appId>\d+<\/appId>/, "<appId>42</appId>"/)
end
)
replace="100"
File.open("xmlfile").each do |line|
if line[/<appId>/ ]
line.sub!(/<appId>\d+<\/appId>/, "<appId>#{replace}</appId>")
end
puts line
end
The right way is to use an XML parsing tool, and example of which is XmlSimple.
You did tag your question with regex. If you really must do it with a regex then
s = "Blah blah <appId>455360226</appId> blah"
s.sub(/<appId>\d+<\/appId>/, "<appId>42</appId>")
is an illustration of the kind of thing you can do but shouldn't.

Resources