-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.

  • I edited one line just to get you started – Jeff Schaller Mar 02 '18 at 19:29
  • Rui F Ribeiro It is not a duplicate it is a different topic. Instead of trolling on my pages, either you can help me out, or troll another page. – XTERMINATOR2018 Mar 02 '18 at 19:42
  • No one properly answered my the previous questions. That is why I wanted to post another topic. If you don't like it, find something more creative to do than trolling on my pages. – XTERMINATOR2018 Mar 02 '18 at 19:46
  • see also https://unix.stackexchange.com/questions/281492/how-to-add-a-new-text-line-at-the-first-line-of-a-file and https://unix.stackexchange.com/questions/284170/replace-a-long-string-with-the-sed-command-argument-list-too-long-error – cas Mar 03 '18 at 07:04

1 Answers1

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.

l0b0
  • 51,350