【发布时间】:2015-01-27 01:39:16
【问题描述】:
让path/to/file 成为包含字符串Hello_ 的文件的路径,该字符串位于大量其他字符的中间位置。使用R,我试图在Hello_ 之后在此文件上打印字符串World,而不删除文件上的任何内容。
【问题讨论】:
-
sed非常适合这类事情,而且速度应该很快。做sed -i '/Hello_/a World' path/to/file
让path/to/file 成为包含字符串Hello_ 的文件的路径,该字符串位于大量其他字符的中间位置。使用R,我试图在Hello_ 之后在此文件上打印字符串World,而不删除文件上的任何内容。
【问题讨论】:
sed 非常适合这类事情,而且速度应该很快。做sed -i '/Hello_/a World' path/to/file
您可以读取文件,添加所需的文本,然后将更新的文本写入磁盘:
# 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")
【讨论】: