1

I have a file named foo where I want to replace the line containing the following regex match:

<script.*src="(.*)".*><\/script>

With the content of a file which name equals the first capture group in the regex above. For example, I might have the following line in foo:

<script type="text/javascript" src="bar.js"></script>

Which I want to replace with the content from the file bar.js.

I want to somehow open the file referenced by the first capture group. This is what I have so far:

sed -r "s/<script.*src=\"(.*)\".*><\/script>/$(cat \1) /" foo

But I am getting cat: 1: No such file or directory.

Even when I try to manually specify the file I want to open I get an Error which I can't understand:

sed -r "s/<script.*src=\"(.*)\".*><\/script>/$(cat bar.js)/" test.txt

Error:

sed: -e expression #1, char 53: unterminated `s' command
Kevin
  • 3,096
  • 2
  • 8
  • 37
  • [Don't Parse XML/HTML With Regex.](https://stackoverflow.com/a/1732454/3776858) I suggest to use an XML/HTML parser (xmlstarlet, xmllint ...). – Cyrus Jul 31 '21 at 16:12
  • `to somehow open the file` What do you mean by "open the file"? You want to replace the line ` – KamilCuk Jul 31 '21 at 16:16
  • @KamilCuk Reading the file. Thanks for the clarified problem description, that is exactly what I am trying to do :) – Kevin Jul 31 '21 at 16:18

1 Answers1

2

First, get what file to open:

file=$(sed -En 's|.*<script.*src="(.*)".*></script>.*|\1|p' test.txt)

Then you can delete the line and read the file in sed using r command and delete line with d.

sed -E -e '\|.*<script.*src="(.*)".*></script>.*|{ r '"$file" -e 'd;}' test.txt

You can use e extension to GNU sed and execute a script instead of the part.

sed -E 's|<script.*src="(.*)".*></script>|cat \1|e' test.txt

You should strongly consider using an XML aware parser, like xmlstarlet or xmllint, and consider using a real programming language, at least scripting one like python or perl.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111