繁体   English   中英

Ruby:特定行的sub / gsub或模式之前/之后

[英]Ruby: sub/gsub at a particular line OR before/after a pattern

我知道我可以在文件中替换以下文本

File.write(file, File.read(file).gsub(/text/, "text_to_replace"))

我们还可以使用sub / gsub来:

  • 替换特定行号上的字符串(在文件中不同位置有相同字符串时有用)

 root@vikas:~# cat file.txt
 fix grammatical or spelling errors
 clarify meaning without changing it
 correct minor mistakes
 add related resources or links
 root@box27:~#

我想在第三行插入一些文字

 root@vikas:~# cat file.txt
 fix grammatical or spelling errors
 clarify meaning without changing it
 Hello, how are you ?
 correct minor mistakes
 add related resources or links
 root@box27:~
  • 匹配模式之前/之后在行上替换字符串

 root@vikas:~# cat file.txt
 fix grammatical or spelling errors
 clarify meaning without changing it
 correct minor mistakes
 add related resources or links
 root@box27:~#

我想搜索“小错误”,然后输入文字“你好,你好吗?”。 在那之前。

 root@vikas:~# cat file.txt
 fix grammatical or spelling errors
 clarify meaning without changing it
 Hello, how are you ?
 correct minor mistakes
 add related resources or links
 root@box27:~

这是答案。

File.open("file.txt", "r").each_line do |line|
  if line =~ /minor mistakes/
     puts "Hello, how are you ?"
  end
  puts "#{line}"
end

这是红宝石单面纸。

ruby -pe 'puts "Hello, how are you ?" if $_ =~ /minor mistakes/' < file.txt

您可以在诸如Thor的宝石中找到此功能。 在此处查看有关inject_into_file方法的文档:

http://www.rubydoc.info/github/erikhuda/thor/master/Thor/Actions#inject_into_file-instance_method

这是该方法的源代码:

https://github.com/erikhuda/thor/blob/067f6638f95bd000b0a92cfb45b668bca5b0efe3/lib/thor/actions/inject_into_file.rb#L24-L32

如果您希望在第n行上匹配(从零开始的偏移量):

def match_line_i(fname, linenbr regex)
  IO.foreach(fname).with_index { |line,i| 
    return line[regex] if i==line_nbr }
end

要么

return scan(regex) if i==line_nbr }

根据您的要求。

如果您希望在给定的行上匹配,则返回前一行,以应用gsub (或其他方法):

def return_previous_line(fname, regex)
  last_line = nil
  IO.foreach(fname) do |line|
    line = f.readline
    return last_line if line =~ regex
    last_line = line
  end
end

如果没有匹配项,则这两个方法均返回nil

好的,因为sub / gsub没有这样的选项,所以我在这里为所有三个选项粘贴我的代码(对宝马代码略作修改)。 希望这可以帮助处于类似情况的人。

  1. 在图案前插入文字
  2. 在图案后插入文字
  3. 在特定的行号处插入文本

     root@box27:~# cat file.txt fix grammatical or spelling errors clarify meaning without changing it correct minor mistakes add related resources or links always respect the original author root@box27:~# root@box27:~# cat ruby_script puts "#### Insert text before a pattern" pattern = 'minor mistakes' File.open("file.txt", "r").each_line do |line| puts "Hello, how are you ?" if line =~ /#{pattern}/ puts "#{line}" end puts "\\n\\n#### Insert text after a pattern" pattern = 'meaning without' File.open("file.txt", "r").each_line do |line| found = 'no' if line =~ /#{pattern}/ puts "#{line}" puts "Hello, how are you ?" found = 'yes' end puts "#{line}" if found == 'no' end puts "\\n\\n#### Insert text at a particular line" insert_at_line = 3 line_number = 1 File.open("file.txt", "r").each_line do |line| puts "Hello, how are you ?" if line_number == insert_at_line line_number += 1 puts "#{line}" end root@box27:~# 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM