Using sed to grep for variable and copy this with an insert text on the same line

Using sed to grep for variable and copy this with an insert text on the same line

I want to grep all title="<text>" variables e.g. title="best-pencil" and copy it on the same line as alt="best-pencil" within a HTML file on Linux:

<p class="images"><img src="my-favorite-pencil.jpg" title="best-pencil">

To:

<p class="images"><img src="my-favorite-pencil.jpg" title="best-pencil" alt="best-pencil">

How can this be done using sed?

答案1

If you can be sure that the string will i) will always be in double quotes and will never contain " and ii) always be on just one line, then it's trivial:

$ sed -E 's/title=("[^"]*")/& alt=\1/' file
<p class="images"><img src="my-favorite-pencil.jpg" title="best-pencil" alt="best-pencil">

The trick is to look for title=" followed by 0 or more non-" until the next " and "capture" this (that's what the parentheses around ("[^"]*") do) so we can then refer to it as \1. In sed, the & symbol means "whatever was matched". So, here, we are replacing title="foo" with itself, followed by alt="foo".

If you have a sed version that doesn't support -E, you can use this instead:

$ sed 's/title=\("[^"]*"\)/& alt=\1/' file
<p class="images"><img src="my-favorite-pencil.jpg" title="best-pencil" alt="best-pencil">

相关内容