I need to insert a hidden HTML input tag into any form tag within a bunch of HTML files.

My idea is to search for any instance of


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"/>'
  • 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

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'
|improve this answer|||||

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" />'
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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