Ruby:sub/gsub 在特定行或 before/after 模式

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:~#

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

 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:~#

我想搜索 'minor mistakes' 并在此之前输入文本 'Hello, how are you ?'。

 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:~

您可以在像 Thor 这样的 gem 中找到此功能。在此处查看 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 }

取决于您的要求。

如果你想匹配给定的行,然后 return 前一行,用于 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

两种方法 return nil 如果没有匹配项。

答案在这里。

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

这是ruby单行。

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

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

  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:~#