Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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


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

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
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

Your Answer


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.