English 中文(简体)
Ruby: How do I selectively remove line wrapping from a text file?
原标题:

I want to edit the following text so that every line begins with Dealer:. This means no wrapping/line breaks. For lines starting with System, wrapping is fine.

What would a solution in ruby look like? Thanks

This is located in a .txt file

Dealer: 5 seconds left to act
Dealer: hitman2714 wins the pot (9)
Dealer: Hand #1684326626D
Dealer: Guitou699 has 5 seconds left to
act
Dealer: Guit¤u699 has timed out
Dealer: baj Hasan has 5 seconds left to
act
Dealer: baj Hasan has timed out
Dealer: hitman2714 has 5 seconds left
to act
Dealer: hitman2714 has timed out
System: The nightly $10,000 guarantee
will be starting in 20 minutes
Dealer: Dealer: Hand #1684326626D
Dealer: Perspextive posts the big
blind of 25

Desired output:

Dealer: 5 seconds left to act
Dealer: hitman2714 wins the pot (9)
Dealer: Hand #1684326626D
Dealer: Guitou699 has 5 seconds left to act
Dealer: Guit¤u699 has timed out
Dealer: baj Hasan has 5 seconds left to act
Dealer: baj Hasan has timed out
Dealer: hitman2714 has 5 seconds left to act
Dealer: hitman2714 has timed out
System: The nightly $10,000 guarantee
will be starting in 20 minutes
Dealer: Dealer: Hand #1684326626D
Dealer: Perspextive posts the big blind of 25

最佳回答
remove_newline = false

ARGF.each_line do |line|
    if line =~ /^(Dealer|System): /
        puts if remove_newline
        remove_newline = ($1 ==  Dealer )
    end
    line.sub!(/
/,    ) if remove_newline
    print line
end

If you don t mind eliminating the wrapping on the "System" lines, and if the file is small enough, you can do something like this:

puts ARGF.readlines.join(  ).gsub(/
(?!Dealer: |System: )/,    )
问题回答
previous =   

ARGF.each_with_index do |line, i|
  line.chomp!
  unless i == 0
    if line =~ /^Dealer/ || line =~ /^System/
      puts previous
      previous = line
    else
      previous << (previous =~ /^System/ ? "
" : " ") << line
    end
  else
    previous = line
  end
end
puts previous

Uses STDIN and STDOUT for input and output.

content.gsub(/^(.*)
(?!(Dealer|System|))/,  1  )

grep

Why use ruby if this kind of tool already exists

cat oldfile | grep ^Dealer: > newfile

ruby

If this is supposed to be part of a bigger ruby application, then there is nothing wrong with doing it in ruby.

content= File.read(oldfile)
content= content.grep(/^Dealer:/)
File.open(newfile, "w") { |f| f.write(content) }




相关问题
Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

multiple ruby extension modules under one directory

Can sources for discrete ruby extension modules live in the same directory, controlled by the same extconf.rb script? Background: I ve a project with two extension modules, foo.so and bar.so which ...

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

热门标签