Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R编写脚本读取txt文件,查找并替换某些文本,连接结果,然后写入文件_R_Search_Replace_Scripting - Fatal编程技术网

R编写脚本读取txt文件,查找并替换某些文本,连接结果,然后写入文件

R编写脚本读取txt文件,查找并替换某些文本,连接结果,然后写入文件,r,search,replace,scripting,R,Search,Replace,Scripting,我有一个问题如下。它将文本文件读入变量linn。第2行(linn[2])和第28行(linn[28])包含两个日期(20160831;20160907)。我想循环一个向量a,一次复制每个元素,替换第2行和第8行上的值,创建一个原始文件的副本,只更改第2行和第8行。然后我想连接所有副本并将其写入txt文件 我怎样才能达到同样的效果 #http://stackoverflow.com/questions/12626637/reading-a-text-file-in-r-line-by-line f

我有一个问题如下。它将文本文件读入变量
linn
。第2行(
linn[2]
)和第28行(
linn[28]
)包含两个日期(20160831;20160907)。我想循环一个向量
a
,一次复制每个元素,替换第2行和第8行上的值,创建一个原始文件的副本,只更改第2行和第8行。然后我想连接所有副本并将其写入txt文件

我怎样才能达到同样的效果

#http://stackoverflow.com/questions/12626637/reading-a-text-file-in-r-line-by-line
fileName <- "C:/Users/500361/Downloads/query.txt"
conn <- file(fileName,open="r")
linn <-readLines(conn)
for (i in 1:length(linn)){
  print(linn[i])
}
close(conn)

str(linn)
linn[2]
#[1] "<"department_code,department_desc,subclass_code,subclass_desc,dept_rank\" label=\"20160831;20160907\">"
linn[28]
#<sel value=\"between(date_id;20160831;20160907) [[ \\ text_req:From \\ text_req:To]] \"/>"


a=c("20160406;20160413","20160330;20160406")
a
#http://stackoverflow.com/questions/12626637/reading-a-text-file-in-r-line-by-line

fileName我找到了答案,想把它发布给未来的读者

#http://stackoverflow.com/questions/23474552/how-to-read-txt-file-and-replace-word-in-r

#variable `a` has different set of values that I want to put in my code (find and replace)
a=c("20160907;20160914","20160831;20160907","20160824;20160831","20160817;20160824","20160810;20160817","20160803;20160810","20160727;20160803","20160720;20160727","20160713;20160720","20160706;20160713","20160629;20160706","20160622;20160629","20160615;20160622","20160608;20160615","20160601;20160608","20160525;20160601","20160518;20160525","20160511;20160518","20160504;20160511","20160427;20160504","20160420;20160427","20160413;20160420","20160406;20160413","20160330;20160406","20160323;20160330","20160316;20160323","20160309;20160316","20160302;20160309","20160224;20160302","20160217;20160224","20160210;20160217","20160203;20160210","20160127;20160203","20160120;20160127","20160113;20160120","20160106;20160113")

res <- readLines("C:/Users/500361/Downloads/query.txt")
result=c()

for (i in 1:36)
  {
  #res1 is just copy of the query 
  res1=res

  #as lines 2 and 28 have the text that I want to replace. I am using gsub function while looping over i
  res1[2]=gsub(pattern = "20160831;20160907", replace = a[i],x=res1[2])
  res1[28]=gsub(pattern = "20160831;20160907", replace = a[i],x=res1[28])


  #concatenating results
  result=c(result,res1)

  }



#writing the file
writeLines(result, con="C:/Users/500361/Downloads/answer.txt")
#http://stackoverflow.com/questions/23474552/how-to-read-txt-file-and-replace-word-in-r
#变量'a'有一组不同的值,我想将它们放入代码中(查找和替换)
a=c(20160907;20160914;20160831;20160907;20160824;20160831;20160817;20160824;20160810;20160817;20160803;20160810;20160727;20160727;20160713;20160720;20160706;20160713;20160629;20160706;20160622;20160629;20160615;20160622;20160608;20160615;20160601;20160608;20160825;20160601;20160518;20160825;20160518;20160518;20160525),"20160511;20160518","20160504;20160511","20160427;20160504","20160420;20160427","20160413;20160420","20160406;20160413","20160330;20160406","20160323;20160330","20160316;20160323","20160309;20160316","20160302;20160309","20160224;20160302","20160217;20160224","20160210;20160217","20160203;20160210","20160127;20160203","20160120;20160127","20160113;20160120","20160106;20160113")

res请记住包含数据,以便我们能够重现(您可以使用
dput()
。这个问题有点不清楚。如果你还提供了一个你想要的结果的例子,那会有帮助。我已经展示了我想要的结果。你可以用正则表达式来做,但是如果数据是可解析的格式,那就更好了。遗憾的是,数据不是可解析的格式,我需要帮助来处理。我不明白为什么会出现这个问题尽管我修改了问题,但还是投了反对票。我不知道这种反对票对我或那些可能计划提供答案的人有什么帮助……我可以进一步修改我的问题
#http://stackoverflow.com/questions/23474552/how-to-read-txt-file-and-replace-word-in-r

#variable `a` has different set of values that I want to put in my code (find and replace)
a=c("20160907;20160914","20160831;20160907","20160824;20160831","20160817;20160824","20160810;20160817","20160803;20160810","20160727;20160803","20160720;20160727","20160713;20160720","20160706;20160713","20160629;20160706","20160622;20160629","20160615;20160622","20160608;20160615","20160601;20160608","20160525;20160601","20160518;20160525","20160511;20160518","20160504;20160511","20160427;20160504","20160420;20160427","20160413;20160420","20160406;20160413","20160330;20160406","20160323;20160330","20160316;20160323","20160309;20160316","20160302;20160309","20160224;20160302","20160217;20160224","20160210;20160217","20160203;20160210","20160127;20160203","20160120;20160127","20160113;20160120","20160106;20160113")

res <- readLines("C:/Users/500361/Downloads/query.txt")
result=c()

for (i in 1:36)
  {
  #res1 is just copy of the query 
  res1=res

  #as lines 2 and 28 have the text that I want to replace. I am using gsub function while looping over i
  res1[2]=gsub(pattern = "20160831;20160907", replace = a[i],x=res1[2])
  res1[28]=gsub(pattern = "20160831;20160907", replace = a[i],x=res1[28])


  #concatenating results
  result=c(result,res1)

  }



#writing the file
writeLines(result, con="C:/Users/500361/Downloads/answer.txt")