tags:

views:

102

answers:

2

i have a file named ama.txt and content of the file is like

name      age   hobby       sex

amit      45    music       male

sumit     35    cricket     female

now i just open like

File.open("ama.txt").each do

end

now how can i modified hobby column of file

A: 
File.readlines("ama.txt").each do |line|
  row = line.strip.split('\t') # use your delimiter symbol
  row[2] = ... # hobby column
end

Or just use fastercsv gem.

Dmitry Maksimov
but its not working file ama.txt remains same .what to do
Amit singh tomar
Dmitry i have to put this code in file.open("ama.txt).each doend ??
Amit singh tomar
Dmitry `File.readlines` returns an array of lines from the file. It doesn't take a block, does it? Are you thinking of `File.foreach` or `File.open().each`?
Telemachus
Tele its not working i want to modify the values of hobby column how to do that??
Amit singh tomar
nd File.open is not returing array of line like when i tried to do like puts "#{line[0]}" i am not getting any value
Amit singh tomar
and one more i wouls like to have these changes must be reflected in ama.txt file
Amit singh tomar
I forgot each, just added it.
Dmitry Maksimov
path="c:/put/ama12.txt" path1="c:/put/ama1.txt" out_file = File.open(path1, 'w') x=0 File.open(path, 'r').each do |line| $row = line.chomp.split("\t") if(x==1) $row[0]="sdrom" line=$row puts end x=x+1 out_file.print "#{line}" end out_file.close
Amit singh tomar
this is what i did but the problem i have is my output is likename age hobby sex sdrom45cricketmale the sencond line should be tab saprated how to do it tele??
Amit singh tomar
+2  A: 

The general pattern for something like this is roughly this:

  1. Open the original file for reading and a new file for writing. Read the original line by line.
  2. Skip any lines that aren't relevant (you want to skip your header line and the blanks, for example)
  3. Deal with the individual lines however you need. Here you probably want something like cols = line.split(/\s+/) and then you want to edit cols[2], though I don't know if the changes fit a pattern or what.
  4. Once you have the edits made, print the edited line out to the new file.
  5. Once you're done with reading the original and writing the changed lines to the new file, close both files.
  6. Change the original's name to something like 'original_name' + '.bak' and the new file's name to 'original_name'.
  7. Done.

Ruby, like most scripting languages, has a lot of built-in ways to make some of these steps go quicker, but that's the basic pattern. Dmitry is potentially right that a CSV reader would help you here, but I can't see how well-formatted (or not) your data is, so I'm not even going to get into that.

Finally, and not to be rude, but it doesn't seem that you know Ruby especially well. Why is Ruby a requirement? Do you know another scripting language? How well do you know Ruby? In general, people here will help you with things, but not simply write code for you.

To answer Amit's question about step 4: If you have a new file open for writing, you will have a filehandle - a variable in your program pointing to the open file. You can write to that file by using the filehandle as a receiver for puts. This looks weird at first, since puts looks like a function normally, but it is a method call in Ruby. Try this in irb or a short Ruby script:

fh = File.open('test.txt', 'w')
fh.puts "hello, world"
fh.close

Another short example:

#!/usr/bin/env ruby
out_file = File.open('output.txt', 'w')

File.open('input.txt', 'r').each do |line|
  out_file.print line.sub('foo', 'bar')
end

out_file.close
# no need to close the input file since the block version closes it for us
# automagically

# Maybe better to put the whole thing in a double block, though you may
# find this harder to follow at first
File.open('output.txt', 'w') do |out_file|
  File.open('input.txt', 'r').each do |line|
    out_file.print line.sub('foo', 'bar')
  end
end
Telemachus
thanks tele but could i get some code means what you wrote its implementation . yaa tele i just started ruby for my colleage project
Amit singh tomar
yaa okk you are saying right noone write code for me thanks for your help
Amit singh tomar
and tele what u said will not work either so next time come up something different
Amit singh tomar
@amit which bit of what Telemachus said wouldn't work?
mikej
i did not get what he said at 4th point mikej
Amit singh tomar
@amit I updated the post to explain point 4 a little.
Telemachus
yaa tele that fh is file handle or object and i used it before but sumthing i wrotepath="c:/put/ama12.txt"File.readlines(path).each do |$line| $row = $line.chomp.split('\t') # use your delimiter symbol $row[2] ="raman"endfh = File.open('test.txt', 'w') fh.puts $rowfh.close but nothing happening for me
Amit singh tomar
@amit Nothing happens because you're trying to do the writing _after_ you've already finished all the reading. Check my outline again. You open both files (the one you read from, and the one you will write to) _earlier_. Then, after you get each line from the one you read, you write it out to the other at the same time. I'll add an example above, but you really need to read your textbook or see your teacher.
Telemachus
yaa tele u r right i have to read textbook
Amit singh tomar