I need to replace several URLs in a text file with some content dependent on the URL itself. Let’s say for simplicity it’s the first line of the document at the URL.
What I’m trying is this:
sed "s/^URL=\(.*\)/TITLE=$(curl -s \1 | head -n 1)/" file.txt
This doesn’t work, since \1 is not set. However, the shell is getting called. Can I somehow push the sed match variables to that subprocess?
So you are trying to call an external command from inside the replacement pattern of a sed substitution. I dont’ think it can be done, the $… inside a pattern just allows you to use an already existent (constant) shell variable.
I’d go with Perl, see the /e option in the search-replace operator (s/…/…/e).
UPDATE: I was wrong, sed plays nicely with the shell, and it allows you do to that. But, then, the backlash in
\1should be escaped. Try instead: