Reputation: 3358
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
Upvotes: 1
Views: 38
Reputation: 141493
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
.
Upvotes: 2