Dmitry Boytsov
Dmitry Boytsov

Reputation: 177

Ruby: each line in different column

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:

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

Upvotes: 4

Views: 285

Answers (2)

Arie Xiao
Arie Xiao

Reputation: 14082

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.

Upvotes: 1

fbonetti
fbonetti

Reputation: 6672

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

Upvotes: 2

Related Questions