【问题标题】:Replace a line in a file using '+' File IO modes in Ruby在 Ruby 中使用“+”文件 IO 模式替换文件中的一行
【发布时间】:2012-04-27 15:22:29
【问题描述】:

这里是 Ruby 初学者!

我知道 Ruby 的 File.open 方法具有某些模式,例如 r,w,a,r+,w+,a+ 和免费的 b。我完全理解 r,w 和 a 模式的使用。但我似乎无法理解如何使用带有“+”符号的那些。谁能给我一些链接,其中有示例以及使用它的解释?

它可以用来读取一行并用等量的内容编辑/替换它吗?如果是,那怎么办?

样本数据文件:a.txt

aaa
bbb
ccc
ddd

Demo.rb

file = File.open "a.txt","r+"
file.each do |line|
  line = line.chomp
  if(line=="bbb")then
  file.puts "big"
  end
end
file.close

我正在尝试用“big”替换“bbb”,但我得到了这个:- 在记事本++中

aaa
bbb
big

ddd

在记事本中

aaa
bbb
bigddd

【问题讨论】:

  • 你需要什么例子?你在r+ 模式下打开文件,你可以读/写它。检查文档,它非常全面。
  • 我冒昧地编辑了您的标题,所以它看起来不像是重复的。
  • 我的回答中包含了您的示例

标签: ruby file file-io


【解决方案1】:

从另一个答案中获取了此文档,所以不是我的,解决方案是我的

r  Read-only mode. The file pointer is placed at the beginning of the file. This is the default mode. 
r+ Read-write mode. The file pointer will be at the beginning of the file. 
w  Write-only mode. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing. 
w+ Read-write mode. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.
a  Write-only mode. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing. 
a+ Read and write mode. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing.

编辑:这里是您的示例的解决方案,大多数情况下,整个字符串被 gsubbed 并写回文件,但也可以在不重写整个文件的情况下替换“infile” 替换成相同长度的字符串要谨慎。

File.open('a.txt', 'r+') do |file|
  file.each_line do |line|
    if (line=~/bbb/)
      file.seek(-line.length-3, IO::SEEK_CUR)
      file.write 'big'
    end
  end
end 

=>
aaa
big
ccc
ddd

这是一种更传统的方式,虽然比大多数其他解决方案更简洁

File.open(filename = "a.txt", "r+") { |file| file << File.read(filename).gsub(/bbb/,"big") } 

EDIT2:我现在意识到这还可以更短

File.write(f = "a.txt", File.read(f).gsub(/bbb/,"big"))

【讨论】:

  • 有没有他们(w+,r+)用法的例子?
  • 因此您将整个文件读入一个变量,然后执行替换,并将变量的内容写回文件。我对吗?我正在寻找某种内联的东西。
  • 为什么要复制粘贴每个文档(文件模式)中已有的内容以及这里的几个类似答案?
  • 很遗憾看到您的答案被否决了,因为您重复了一些文档,而它是解决方案的全部内容,OP 要求不要替换和重写整个文件,只是其中的一部分,这正是这确实..
  • Jazz,因为你可能在某处有额外的空格、换行或返回字符,无论如何,我只会使用这种方法对非常大的文件进行操作,这样就不必复制它们,我添加了一个更常见的解决方案,其中文件被完全重写但不使用第二个文件。希望这对你来说已经足够好了
【解决方案2】:

所以你正在将整个文件读入一个变量,然后执行 替换,并将变量的内容写回 文件。我对吗?我正在寻找一些内联的东西

这就是这样做的方法。您可以交替使用IO#readlines 将所有行读入Array,然后对其进行处理。

这已经回答了:

How to search file text for a pattern and replace it with a given value

如果您担心性能或内存使用情况,请使用正确的工具来完成正确的工作。在*nix(或 Windows 上的 cygwin)上:

sed -i -e "s/bbb/big/g" a.txt

会做你想做的事。

【讨论】:

  • 你会认真安装 cygwin 来在 windows 上进行字符串替换吗? Ruby 本身足够快且足够强大,如果您需要外部工具,有很多 Windows 工具可供选择
  • @peter 我在哪里暗示你需要安装 cygwin 来完成这个任务?很多人已经有了。至于 windows 工具,可能是真的,鼓励你举个例子。
  • 我想你是 nix 用户? 8>)。一个例子:在你可以使用的 ruby​​\bin 映射中有一个 sed.exe
  • Ernest:问题,你如何将@Ernest 放在评论的开头,对我不起作用
  • 在 Windows 上工作时无法使用“sed”。是的,我不想创建新文件,只想更改现有文件中的一些内容。
猜你喜欢
  • 1970-01-01
  • 2016-06-27
  • 1970-01-01
  • 2013-06-17
  • 1970-01-01
  • 1970-01-01
  • 2013-06-26
  • 1970-01-01
  • 2013-09-02
相关资源
最近更新 更多