Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用'替换文件中的一行+';Ruby中的文件IO模式_Ruby_File_File Io - Fatal编程技术网

使用'替换文件中的一行+';Ruby中的文件IO模式

使用'替换文件中的一行+';Ruby中的文件IO模式,ruby,file,file-io,Ruby,File,File Io,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| li

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
我试图用“大”来代替“bbb”,但我得到了以下信息:- 在记事本里++

aaa
bbb
big

ddd
在记事本里

aaa
bbb
bigddd

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

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.
编辑:这里是示例的解决方案,大多数情况下,整个字符串都被G subbed并写回文件,但也可以在不重写整个文件的情况下进行“infle”替换 您应该谨慎地使用相同长度的字符串替换

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") } 

因此,您正在将整个文件读入一个变量,然后执行 替换,并将变量的内容写回 文件我说得对吗?我在找一些类似内联的东西

这就是方法。您可以交替使用
IO#readlines
将所有行读入
数组
,然后进行处理

这已经得到了回答:

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


将完全满足您的要求。

您需要哪些示例的可能副本?您可以在
r+
模式下打开文件,并可以读/写它。检查文档,它非常全面。我随意编辑了您的标题,使其看起来不那么像副本。我在回答中包含了您的示例它们的(w+,r+)用法的任何示例?因此您将整个文件读入一个变量,然后执行替换,并将变量的内容写回文件。我说得对吗?我一直在寻找一些内联的东西。为什么要复制粘贴每个文档(文件模式)中已经存在的内容和这里的几个类似答案?看到你的答案被否决是令人难过的,因为你重复了一些文档,而这是解决方案,OP要求不要替换和重写整个文件,Jazz,因为你可能在某个地方有额外的空格、行或返回字符,无论如何,我只会用这个方法对非常大的文件进行操作,这样就不必复制它们了,我添加了一个更常见的解决方案,即完全重写文件,但不使用第二个文件。希望这对您来说足够好您会认真安装cygwin在windows上进行字符串替换吗?Ruby本身速度快且功能强大,如果您需要外部工具,有很多windows工具可供选择from@peter我在哪里暗示您需要将cygwin安装到此任务?很多人已经有了。至于windows工具,可能是真的,我们鼓励您举个例子。我想您是nix用户吧?8>). 例如:ruby\bin映射中有一个sed.exe,您可以使用它。est:question,您如何将@Ernest放在注释的开头,与meCannot use'sed'不兼容,因为我在windows上工作。是的,我不想创建新文件,只想更改现有文件中的某些内容。
File.write(f = "a.txt", File.read(f).gsub(/bbb/,"big"))
sed -i -e "s/bbb/big/g" a.txt