【问题标题】:How to read a file and delete a string from a particular line如何读取文件并从特定行中删除字符串
【发布时间】:2017-05-19 09:44:11
【问题描述】:

我有以下文件:


ids_to_remove.txt

ID  File_Name   Row Name

id\a_po87y  GMT_dealer  auto_dev
id\ruio66   dao_wells   auto_dev
id\rzd766   123_Smart_option    cia_read
...
...
etc

GMT_dealer-details.txt

#This configuration file was written by: org.eclipse.equinox.internal

[groups]

auto_dev = id\a_po87y, id\rt7890, id\sdfs09, id\rzdo9k
qa_check = id\op9iu, id\guijm0, id\a_po87y
AD_read = id\a_po87y

dao_wells-details.txt

#Content may be structured and packaged into modules to facilitate delivering, extending, and upgrading the Content

[groups]

AD_read = id\a_po87y
auto_dev = id\guijm0, id\oikju8, id\ruio66
CSI = id\kiopl, id\ruio66, id\o9i8u7

ids_to_remove.txt 中,有接近 500 个条目,并且一行中的项目是制表符分隔的。在其他文件中,组值以空格分隔。

File_Name 的值表示位于E:/my_files/*-details.txt 的文件夹或文件,其中*File_Name 值,例如GMT_dealerdao_wells123_Smart_option

对于每一行,我想在File_Name 表示的文件中删除行ID 为Row Name 的行中出现的任何ID 值。例如,我想从文件GMT_dealer 中id 为auto_dev 的行中删除字符串id\a_po87yid\a_po87y 应仅从组auto_dev 中删除,组qa_checkAD_read 中存在的相同ID 应保持原样。同样,它必须对E:/my_files 下的所有文件执行。

我写了下面的代码:

file_dir = 'E:/my_files'

file = File.open("E:/ids_to_remove.txt", "r")
contents = file.each_line.map { |line| line.split("\t") }.transpose
id, file_name, group  = contents


id.each do |ids|
  puts "For id: #{ids}"
  file_name.each do |name| 
   value = File.open("#{file_dir}/#{name}-details.txt")
   text = File.read(value)

   text.each_line do |el|
    group.each do |gr|
     if el.match(/#{gr}/) then 
      print "group row #{gr}\n"
      replace = text.gsub( /#{Regexp.escape(ids)}\,\s/, '').gsub( /#{Regexp.escape(ids)}/, '' ).gsub /,\s*$/, ''
     end
     group.shift
    end 
   end
   file_name.shift
  end
end  
id.shift

它不能满足我的需要。寻找任何建议。

为了调试添加了一些 put 和我得到的输出

For ID: id\a_po87y
group row auto_dev

group row auto_dev

For ID: id\ruio66
For ID: id\rzd766

【问题讨论】:

  • sawa - 感谢您的编辑,我是这个论坛的新手,正在接受调整。你能帮我解决这个问题吗?谢谢
  • “它没有做我需要的事情”——它做了什么?
  • @SergioTulentsev ,感谢您的调查。它不会替换目标文件中的字符串。也没有错误。不知道我应该在哪里进行更改。
  • group.shiftfile_name.shift 怎么了?你需要它们做什么?
  • @SergioTulentsev ,我正在尝试删除所有 3 个数组的第一个元素 - idgroupfile_name 一旦第一个 id 或字符串在相应行的第一个文件中被删除。我的逻辑可能有误。请寻找您的专业建议。

标签: ruby


【解决方案1】:

我会这样做:

file_dir = 'E:/my_files'
file = File.open("E:/ids_to_remove.txt", "r")

file.each_line.map do |line| 
  id, file_name, group = line.split

  old_text = File.read("#{file_dir}/#{file_name}-details.txt")
  new_text = []

  old_text.each_line do |line|
    if line =~ /=/
      line_group, line_ids = line.split("=")

      if line_group.strip == group.strip
        line_ids = line_ids.split(",").reject { |l_id| l_id.strip == id }.join(",")
      end

      new_text << "#{line_group}=#{line_ids.chomp("\n")}"
    else
      new_text << line.chomp("\n")
    end

  end

  File.write("#{file_dir}/#{file_name}-details.txt", new_text.join("\n"))
end

我确信有更好的方法来处理额外的"\n",但这将获得所需的输出。

【讨论】:

  • 太棒了,为什么我没有像你一样思考:) 非常感谢。
猜你喜欢
  • 2013-08-08
  • 2019-05-17
  • 1970-01-01
  • 1970-01-01
  • 2018-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-19
相关资源
最近更新 更多