从文件中读取行并将它们压缩在一起

Reading lines from a file and condensing them together

我正在读取一个文件,其中包含以下信息:

  Organization: QC
  Company: Luxury Mortgage Corp. (0020)
  Folio: 3366326
  Doc Code: QCMAIL_STMT         
  Sequence: 3
  Pages: 7
  Method: SCAN            
  User: LAS             
  Received: 01/20/2016

我正在尝试从文件中提取行,并且只使用 FolioSequencePagesUser

然而,当我这样做时,它显示如下:

Folio: 
Sequence: 
Page: 
User: 
Folio: 
Sequence: 
Page: 
User: 
Folio: 
Sequence: 
Page: 
User: 
Folio: 
Sequence: 
Page: 
User: 
Folio: 3366326
Sequence: 
Page: 
User: 
Folio: 
Sequence: 
Page: 
User: 
Folio: 
Sequence: 3
Page: 
User: 
Folio: 
Sequence: 
Page: 7
User: 
Folio: 
Sequence: 
Page: 
User: 
Folio: 
Sequence: 
Page: 
User: LAS

我需要的是让它像这样显示:

Folio: 3366326
Sequence: 3
Page: 7
User: LAS

来源:

#!/usr/local/bin/ruby

require 'colored'

class UncommittedDocs

  #attr_accessor :file

 # def initialize(username)
  #  @file = file
 # end

  def pull_info
    File.open("/home/qc/tep/bin/ruby/uncomitted.txt", "r").each_line do |line|
      puts "Folio: #{line.split(" ")[1] if line.include?("Folio")}"
      puts "Sequence: #{line.split(" ")[1] if line.include?("Sequence")}"
      puts "Page: #{line.split(" ")[1] if line.include?("Pages")}"
      puts "User: #{line.split(" ")[1] if line.include?("User")}"
    end
  end
end

test = UncommittedDocs.new#"/home/qc/tep/bin/ruby/uncommittedddocstest.txt")
test.pull_info

if 移出字符串插值,以便它应用于 puts 调用:

puts "Folio: #{line.split(" ")[1]}" if line.include?("Folio")

你应该没事的。也就是说,直到您遇到包含关键字的数据,例如

  User: Mr. Folio Pages

为避免该问题,请使用更严格的条件。您越了解文件的格式,就越能定制它。如果您没有任何规范,而只有上面的示例文件内容可以使用,请选择仍然健壮的东西,例如

puts "Folio: #{line.split(" ")[1]}" if line.strip.start_with?("Folio:")

你的条件放错了。

使用这个:

puts "Folio: #{line.split(" ")[1] if line.include?("Folio")}"

而不是这个:

puts "Folio: #{line.split(" ")[1]}" if line.include?("Folio")

看起来你正在用 line.split 等做很多工作,当你真的只想打印符合特定条件的行并跳过其余部分时:

class UncommittedDocs
  MATCH_LINE_EXPR = /^(Folio|Sequence|Page|User):/

  def pull_info
    File.open("/home/qc/tep/bin/ruby/uncomitted.txt", "r").each_line do |line|
      puts line if line =~ MATCH_LINE_EXPR
    end
  end
end

test = UncommittedDocs.new
test.pull_info

如果有您要去除的前导空格,则可以进行以下更改:

MATCH_LINE_EXPR = /^\s*(Folio|Sequence|Page|User):/

def pull_info
  File.open("/home/qc/tep/bin/ruby/uncomitted.txt", "r").each_line do |line|
    puts line.lstrip if line =~ MATCH_LINE_EXPR
  end
end

这只是在正则表达式的开头添加 \s*(以匹配零个或多个前导空白字符)并将 puts line 更改为 puts line.lstrip 以从匹配中去除任何前导空白行。

要在 User: 行之后添加一个空行,根据您的评论,添加另一个 puts 仅在这种情况下才会执行。因为我们已经在捕获组 1 中捕获 Folio|Sequence|Page|User,所以这很简单:

def pull_info
  File.open("/home/qc/tep/bin/ruby/uncomitted.txt", "r").each_line do |line|
    puts line if line =~ MATCH_LINE_EXPR
    puts if  == "User"
  end
end

如果您的输入文件是 YAML(看起来可能是),请使用 Ruby's built-in YAML support:

require 'yaml'
require 'active_support/core_ext/hash/slice'  # For comfortably dissecting a Hash
                                 # See 

# ...

  def pull_info
    data = YAML.load_file('/home/qc/tep/bin/ruby/uncomitted.txt')
    puts data.slice('Folio', 'Sequence', 'Pages', 'User').to_yaml
  end

# ...
IO.foreach('/path/to/file.txt') { |l| puts l if l =~ /[Folio|Sequence|Pages|User]: \w/ }