如何在文件中找到一个字符串并在它之后立即打印?

How to find a string on a file and print right after it?

path/to/file 为包含字符串 Hello_ 的文件的路径,该字符串位于大量其他字符的中间某处。使用 R,我试图在 Hello_ 之后立即在此文件上打印字符串 World,而不删除文件上的任何内容。

您可以读取文件,添加所需的文本,然后将更新后的文本写入磁盘:

# Read text
file1 = readLines("pathToFile/test_file.txt")

# Add "World" after each instance of "Hello_"
file1 = gsub("(Hello_)","\1World", file1)

# Write updated text to a new file (you can overwrite the existing file 
#  instead if you wish).
writeLines(file1, "pathToFile/test_file_updated.txt")