Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I need to insert a hidden HTML input tag into any form tag within a bunch of HTML files. I assume this is possible with sed, but need help forming the command.

My idea is to search for any instance of

<form

and if found, insert a line below it that contains:

<input type="hidden" name="csrf_token" value="$csrf_token" /> 

What's the best way to tackle this? I'm close with

sed -e '/<form/a\<input type="hidden" name="csrf_token" value="$csrf_token"/>'
share|improve this question
    
Why do you add a as replace part ? Do you need it ? –  Patryk Jan 23 '13 at 15:54
    
@Patryk a\ appends the text on the newline. –  mmattax Jan 23 '13 at 19:46
    
Well, somehow \a doesn't work for me. But \n works like a charm. –  Patryk Jan 24 '13 at 8:25
add comment

2 Answers

To add something below each <form you have to add & to indicate that you want to keep the caught sequence (plus \n as you mentioned you need it) :

$ find *.html | xargs sed -ie 's/<form/&\n\<input type="hidden" name="csrf_token" value="$csrf_token"\/>/g'
share|improve this answer
add comment
up vote 1 down vote accepted

Got it. Here is how it's done:

find . -name \*.html | xargs sed -i '/<form/a\<input type="hidden" name="csrf_token" value="$csrf_token" />'
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.