【问题标题】:How to find a string on a file and print right after it?如何在文件中查找字符串并在其后立即打印?
【发布时间】:2015-01-27 01:39:16
【问题描述】:

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

【问题讨论】:

  • sed 非常适合这类事情,而且速度应该很快。做sed -i '/Hello_/a World' path/to/file

标签: r string file text


【解决方案1】:

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

# 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")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-17
    • 2012-08-10
    • 2012-12-06
    • 2013-12-20
    • 1970-01-01
    • 2014-02-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多