0

I have a template that looks like:

<mydata>
<tag1> 
<tag2> etc.
DATAHERE
</mydata>

I want to run a query on a DB and fetch a number of records and place them one below the other in the file at the place where there is the string DATAHERE.

My records are already fetched in mydata.txt. How do i replace the single line of DATAHERE ? I do not want to hardcode the number of lines in the template to skip. DATAHERE is my only marker.

Yash
  • 946
  • 1
  • 13
  • 28
  • [Don't Parse XML/HTML With Regex.](https://stackoverflow.com/a/1732454/3776858) I suggest to use an XML/HTML parser (xmlstarlet, xmllint ...). – Cyrus Aug 18 '19 at 10:41

1 Answers1

3

This might work for you (GNU sed):

sed -e '/DATAHERE/{r dataFile' -e 'd}' file

Focus on the line DATAHERE and read the file dataFile then delete the current line.

potong
  • 55,640
  • 6
  • 51
  • 83
  • 1
    `sed -e '/DATAHERE/r dataFile' -e '/DATAHERE/d' file` should work with any sed. Or `sed -e '/DATAHERE/{r dataFiled}' file` – Shawn Aug 19 '19 at 02:39