[英]Ruby: sub/gsub at a particular line OR before/after a pattern
I know that I can replace text as below in a file 我知道我可以在文件中替换以下文本
File.write(file, File.read(file).gsub(/text/, "text_to_replace"))
Can we also use sub/gsub to:- 我们还可以使用sub / gsub来:
Example 例
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:~#
I want to insert some text at 3rd line 我想在第三行插入一些文字
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:~
Example 例
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:~#
I want to search 'minor mistakes' and put text 'Hello, how are you ?' 我想搜索“小错误”,然后输入文字“你好,你好吗?”。 before that.
在那之前。
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:~
Here is the answer. 这是答案。
File.open("file.txt", "r").each_line do |line|
if line =~ /minor mistakes/
puts "Hello, how are you ?"
end
puts "#{line}"
end
Here is ruby one-liner. 这是红宝石单面纸。
ruby -pe 'puts "Hello, how are you ?" if $_ =~ /minor mistakes/' < file.txt
You can find this functionality in a gem like Thor. 您可以在诸如Thor的宝石中找到此功能。 Check out the documentation for the
inject_into_file
method here: 在此处查看有关
inject_into_file
方法的文档:
http://www.rubydoc.info/github/erikhuda/thor/master/Thor/Actions#inject_into_file-instance_method . http://www.rubydoc.info/github/erikhuda/thor/master/Thor/Actions#inject_into_file-instance_method 。
Here is the source code for the method: 这是该方法的源代码:
https://github.com/erikhuda/thor/blob/067f6638f95bd000b0a92cfb45b668bca5b0efe3/lib/thor/actions/inject_into_file.rb#L24-L32 https://github.com/erikhuda/thor/blob/067f6638f95bd000b0a92cfb45b668bca5b0efe3/lib/thor/actions/inject_into_file.rb#L24-L32
If you wish to match on line n
(offset from zero): 如果您希望在第
n
行上匹配(从零开始的偏移量):
def match_line_i(fname, linenbr regex)
IO.foreach(fname).with_index { |line,i|
return line[regex] if i==line_nbr }
end
or 要么
return scan(regex) if i==line_nbr }
depending on your requirements. 根据您的要求。
If you wish to match on a given line, then return the previous line, for application of gsub
(or whatever): 如果您希望在给定的行上匹配,则返回前一行,以应用
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
Both methods return nil
if there is no match. 如果没有匹配项,则这两个方法均返回
nil
。
Okay, as there is no such option available with sub/gsub, I am pasting here my code (with slight modifications to BMW's code) for all three options. 好的,因为sub / gsub没有这样的选项,所以我在这里为所有三个选项粘贴我的代码(对宝马代码略作修改)。 Hopefully, this helps someone in a similar situation.
希望这可以帮助处于类似情况的人。
Insert text at a specific line number 在特定的行号处插入文本
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.