-2

I have a query as below. It reads a text file into variable linn. Line 2 (linn[2]) and line 28 (linn[28]) contain 2 dates (20160831;20160907). I would like to loop over a vector a and copy each element at a time and replace those values on lines 2 and 8 and create a copy of the original file with only line 2 and line 8 changed. Then I would like to concatenate all the copies and write it into a txt file

How could I achieve the same?

#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

update 1: updated values in line 2 and 28 will be

 "<"department_code,department_desc,subclass_code,subclass_desc,dept_rank\" label=\"20160406;20160413\">"
      #<sel value=\"between(date_id;20160406;20160413) [[ \\ text_req:From \\ text_req:To]] \"/>"

 "<"department_code,department_desc,subclass_code,subclass_desc,dept_rank\" label=\"20160330;20160406\">"
      #<sel value=\"between(date_id;20160330;20160406) [[ \\ text_req:From \\ text_req:To]] \"/>"
user2543622
  • 5,760
  • 25
  • 91
  • 159
  • Please remember to include the data so that we can reproduce this (you can use `dput()`. The question is kind of unclear. It would help if you also provided an example of your desired result. – Hack-R Sep 26 '16 at 23:14
  • i have shown my desired results – user2543622 Sep 26 '16 at 23:20
  • You could do it with regex, but if the data is in a parseable format, that's preferable. – alistaire Sep 26 '16 at 23:21
  • sadly the data is not in parseable format and i would need help to proceed – user2543622 Sep 26 '16 at 23:26
  • I dont understand why this question has been down voted even though i modified the question. I have no idea how that down voting going to help me or people who might be planning to provide an answer...i can modify my question further – user2543622 Sep 27 '16 at 02:53

1 Answers1

0

I found the answer and wanted to post it for future readers

#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")
user2543622
  • 5,760
  • 25
  • 91
  • 159