4

I am a newcomer in Ruby and I have the following code:

out_file = File.open('new1.csv', 'w')
File.open("new7.txt").each do |line| 
  if line =~ /Revision/ then
    out_file.puts line
  elsif
    line =~ /Author/ then
    out_file.puts line
  elsif
    line =~ /Date/ then
    out_file.puts line
  end
end

I need:

  • The line with "Revision" put in column A in the output CSV file
  • The line with "Author" put in column B
  • The line with "Date" put in column C
  • And so on

Can anyone show me how to put the data in columns as described?

Right now all lines are put in one row.

The sample of "new7.txt"

Revision: 37407
Author: imakarov
Date: 21 June 2013 г. 10:23:28
Message:
update specification from Jhon (it was in VTBSOATST-1219)
----
Added : /Analitics/Документы/ЧТЗ/BR-5610/2 Спецификации/BR-5610 Публикация клиентских данных в АБС Бисквит (CifOraSyncOffPers).docx
Deleted : /Analitics/Документы/ЧТЗ/BR-5610/2 Спецификации/BR-5610 Публикация клиентских данных в АБС Бисквит.docx

Revision: 37406
Author: imakarov
Date: 21 June 2013 г. 10:22:16
Message: 
delete files

----
Deleted : /Analitics/Документы/ЧТЗ/BR-5610/2 Спецификации/ЧТЗ Принудительное обновление и публикация ФЛ с замечаниями Кочебина С..docx
Deleted : /Analitics/Документы/ЧТЗ/BR-5610/2 Спецификации/ЧТЗ Принудительное обновление и публикация ФЛ-comments.docx
Deleted : /Analitics/Документы/ЧТЗ/BR-5610/2 Спецификации/ЧТЗ Принудительное обновление и публикация ФЛ-comments_Орлов.docx
Deleted : /Analitics/Документы/ЧТЗ/BR-5610/2 Спецификации/ЧТЗ Принудительное обновление и публикация ФЛ.docx

Revision: 37405
Author: dboytsov
Date: 21 June 2013 г. 10:21:17
Message:
add attributes in file
----
Modified : /Analitics/Документы/ЧТЗ/BR-5864 Запрос данных клиента по интернет-анкете КН/Преобразование BR-5864.docx
Modified : /Analitics/Документы/ЧТЗ/BR-5864 Запрос данных клиента по интернет-анкете КН/ЧТЗ BR-5864 Запрос данных клиента по интернет анкете.docx

May be it would be a better way to export in .xls ? Is it a difficult to export in .xls file in each column inside?

Now I have the following situation: enter image description here

But I need that: enter image description here

3
  • 2
    Can you post a sample of your source file ('new7.txt')? Commented Jun 21, 2013 at 23:21
  • 1
    Without a sample of your input file, we can only guess about what you need. That's not going to do you any good and will waste our time. Commented Jun 22, 2013 at 2:10
  • @the Tin Man I am sorry. Now I Update post.Add sample Commented Jun 22, 2013 at 8:03

2 Answers 2

2

Use the csv library. Assuming that new7.txt has the column order of Author, Revision, Date, you can do the following:

require 'csv'

# parse the csv file into an array

CSV.parse("new7.txt", {:headers => false}).each do |line|

  # assign each 'cell' to a variable

  auther = line[0]
  revision = line[1]
  date = line[2]

  # append the newly order data onto a new csv file

  CSV.open("new7_revised.txt", "a") do |csv|
    csv << [revision, author, date]
  end
end
Sign up to request clarification or add additional context in comments.

3 Comments

This doesn't seem to match the question, where the input data in new7.txt isn't in the CSV format. Some lines contain "Revision", some lines contain "Author", etc.
Ah, I thought that the OP wanted the columns to be reordered.
@DarshanComputing Yes, you are absolutelu right. The input 'new7.txt' file is not a csv, it's just a txt. And Inside it I have such words as "Revision" "Author" "date" and so on. In input file it is not a column. And I want to parse the input file and copy to csv by columns
1

Given the information provided by the OP,

require 'csv'

data = []
File.foreach("new7.txt") do |line|
  line.chomp!
  if line =~ /Revision/
    data.push [line]
  elsif line =~ /Author/
    if data.last and not data.last[1]
      data.last[1] = line
    else
      data.push [nil, line]
    end
  elsif line =~ /Date/
    if data.last and not data.last[2]
      data.last[2] = line
    else
      data.push [nil, nil, line]
    end
  end
end

CSV.open('new1.csv', 'w') do |csv|
  data.each do |record|
    csv << record
  end
end
  • Should work well for lines in sequence: Revision, Author, Date, Revision, Date, Author, Revision, ...
  • If lines are not well ordered:
    • Revision line is considered as the start of a new record
    • If there isn't a Revision line between 2 Author (or Date) line, the second Author (or Date) line is considered to be in a new record.

4 Comments

thank you a lot! I've tried, but now it puts all in one row. But I need to puts in every column seperately.
@DmitryBoytsov I've replaced File.open to CSV.open when writing the CSV file. Each record will be written in one line separately, rather than all joined in a single line.
I've checked with the sample data you provided, it should work as desired. Note that you need to store the data in a CSV file with the csv library, as the code I wrote.
Ok,I've tried again :) but what if I don't need to join in a single line?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.