-3

Due to some problems in this site when it comes to displaying. I am using '{' instead of '<'.

Here is my sed command:

sed 's/{head/*backbutton_and_scroller_script.txt*{head/' report.usc20.txt

I want to insert text information from inside 'backbutton_and_scroller_script.txt' into 'report.usc20.txt' file in front of the head tag. How can I properly point towards the backbutton_and_scroller_script.txt file in my sed command? My current code is displaying the file name backbutton_and_scroller_script.txt infront of the head tag inside report.usc20.txt rather than the text that is inside the backbutton_and_scroller_script.txt.

marked as duplicate by Rui F Ribeiro, Jeff Schaller, l0b0, Sparhawk, G-Man Mar 3 '18 at 3:32

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

0

First, you seem to have some misunderstandings of how sed works. sed doesn't have a "redirect file into this part of the pattern" operator. If you could redirect anything in there you would then have to escape any special characters in the input text to avoid messing up the pattern (this includes characters like dot and slash, so not exactly uncommon ones). And to modify a file you need to use the -i option to sed.

This may not be what you want to hear, but sed is the wrong tool for this job. The best thing you can do is get a tool or library which understands this type of markup (such as XSLT or a Python/Java/Ruby library).

If you are using some completely custom language you can use a horrible hack to read the file up to the first occurrence of {head:

file="$(<report.usc20.txt)"
start="${file%%\{head*}"

, get the corresponding rest of the file:

end="${file#*\{head}"

and finally chuck all the pieces together:

printf '%s%s{head%s' "$start" "$(<backbutton_and_scroller_script.txt)" "$end" > report.usc20.txt

This is untested and horrible and I recommend using a proper programming language instead.

  • Hey. I found the right way to do it. But thanks anyways for the help. – XTERMINATOR2018 Mar 5 '18 at 21:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.