sed pattern matching or passing variables

I need sed to add a "/>" to the end of a line that contains/starts with <meta.

current line is

<meta name="keywords" content="kayword 1, kwyword2">

and should be

<meta name="keywords" content="kayword 1, kwyword2 " />

i need something like this?

find . -name "*.html" -print0 | xargs -0 sed -i 's/<meta *>/<meta * \/>/g'

any help would be appreciated :slight_smile:

Try:

 sed -n '/^<meta/p' test | sed 's\>\ />\g' 

(sed takes only the lines starting with <meta and then changes > to /> )

....note that "test" must be your own file...

Another approach:

sed 's!\(<meta.*\).$!\1 />!'

Regards

I tried

's!\(<meta.*\).$!\1 />!'

with find like this

find . -name "*.html" -print0 | xargs -0 sed -i 's!\(<meta.*\).$!\1 />!'

It works,but the result contains two ">" like this...

<meta name="keywords" content="keyword1, keyword2"> />

i could run another sed to replace "> />", but if it is a easy improvement in the find section above?

I played with JCastro's example, but could not get it to work with find piped to sed

I am close and have enough to play further, but any further assistance would be great.

This works...

find lib/lib-pal -name "*.html" -print0 | xargs -0 sed -i 's!\(<meta.*\).$!\1 />!;s/"> \/>/" \/>/g'

Thanks

The following uses regular expression to remove any unlisted characters (including ">") and seems to be more predictable.

find . -name "*.html" -print0 | xargs -0 sed -i s/\(<meta name[a-zA-Z \=\"\,\.\0-8\&\;\@-]*\).*/\1 \/>/