簡體   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