0

I have this HTML page (template.html) & I want to extract the value of input form_nonce

       <div class="row">
                                    <div class="col s12 input-field">
                                        <input type="hidden" name="form_nonce" id="reset_form_nonce" value="66eef7c75d28e44817959c8eec1e0074"/>
                                        <input type="text" placeholder="" name="form_login" id="reset_form_login" class="input" value="" size="20"
                                               autocorrect="off" autocapitalize="none"
                                               tabindex="10"/>
                                        <label for="reset_form_login"><i class="icon-user icon"></i> Username or Email</label>
                                    </div>
                                </div>
<div class="row">
                                    <div class="col s12 input-field">
                                        <input type="hidden" name="form_nonce" id="reset_form_nonce" value="66eef7c75d28e44817959c8eec1e0074"/>
                                        <input type="text" placeholder="" name="form_login" id="reset_form_login" class="input" value="" size="20"
                                               autocorrect="off" autocapitalize="none"
                                               tabindex="10"/>
                                        <label for="reset_form_login"><i class="icon-user icon"></i> Username or Email</label>
                                    </div>
                                </div>

I used the following command:

NONCE=`grep -m 1 "form_nonce" template.html | awk -F '"' '{print $8}'`

But the problem is that I don't always receive the input in this format so sometimes it's 7 , 8 or 9. Is there a way to focus on the value keyword instead of assuming the order of the value?

Edit: There are multiple input with name="form_nonce"

3
  • may be grep -Po 'name="form_nonce".*value="\K[^"]+' Commented Apr 24, 2018 at 9:19
  • remove -m 1 to continue search for more than one pattern Commented Apr 24, 2018 at 11:23
  • Best way seems to be an html parser (and there are plenty such parsers available). Why use bash to do it ? Commented Apr 24, 2018 at 14:54

1 Answer 1

1

Using grep and sed

grep -m 1 "form_nonce" template.html |sed -e 's/.*value="//'|sed -e 's/"\/>//'

In the first sed substitution everything upto value=" is replaced and in second sed ending tag and quotes are replaced.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.