Ruby:sub/gsub位于特定行或模式之前/之后
我知道我可以在文件中替换如下文本Ruby:sub/gsub位于特定行或模式之前/之后,ruby,string,design-patterns,replace,gsub,Ruby,String,Design Patterns,Replace,Gsub,我知道我可以在文件中替换如下文本 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
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
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
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:~
你可以在像雷神一样的宝石中找到这个功能。请在此处查看
将\u插入\u文件方法的文档:
以下是该方法的源代码:
如果您希望在线匹配n
(从零开始的偏移):
或
根据您的要求
如果希望在给定行上进行匹配,则返回前一行,用于应用gsub
(或其他任何内容):
如果没有匹配项,这两种方法都会返回nil
File.open("file.txt", "r").each_line do |line|
if line =~ /minor mistakes/
puts "Hello, how are you ?"
end
puts "#{line}"
end
这是ruby one liner
ruby -pe 'puts "Hello, how are you ?" if $_ =~ /minor mistakes/' < file.txt
ruby-pe'如果$\ux=~/minor errors/'
好的,因为sub/gsub没有这样的选项,所以我将这三个选项的代码粘贴到这里(对BMW的代码稍作修改)。希望这能帮助处于类似情况的人
在图案前插入文本
在图案后插入文本
在特定行号处插入文本
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:~#
您希望给出一些输入示例并期望输出。@我不是在寻找一个特定的示例,而是一个通用方法。类似于Unix中的“sed”命令。现在看起来不错,请参阅我的answer@BMW完美的我以为会有像sed和sub/gsub这样的一行程序。感谢您为此付出的时间和努力。我还在你的代码中粘贴修改,使其能够在模式匹配后以及在特定行插入文本。好消息。如果答案是这样的话,你需要投票或者接受它。
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:~#