0

This is the code :

#!/bin/bash
head=`curl -D -s "http://example.com/" | grep "j_id__v_0:javax.faces.ViewState:2"`
echo $head

and the output in terminal :

            </script>
            <input type="hidden" name="GlobalFooter_SUBMIT" value="1" />
            <input type="hidden" name="javax.faces.ViewState" id="j_id__v_0:javax.faces.ViewState:2" value="V7wZDq4cDizFPZ0i52hQGUD25XgWp5NJC+hCql33eTTwC2hm" autocomplete="off" />
        </form>
    </div>
</div> 

How do I print the contents of the value?

Nic3500
  • 8,144
  • 10
  • 29
  • 40
  • 2
    You can use an HTML parser. – oguz ismail Oct 20 '18 at 19:05
  • 1
    https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 , but maybe also just `... | grep -o 'value="\K[^"+)"'` , or if your `grep` doesn't understand that, `perl -lne 'print $1 if /value="([^"]+)"/'` – Corion Oct 20 '18 at 19:40
  • Print to file? Just `cat` it with `>`. – T.Woody Oct 20 '18 at 20:43

1 Answers1

0

Add a sed after the grep to keep only the value:

#!/bin/bash

head=$(curl -D -s "http://example.com/" | grep "j_id__v_0:javax.faces.ViewState:2" | sed 's/.*\svalue="\(\S*\)"\s.*/\1/')
echo $head

Couple points:

  • use the $() syntax to assign commands results to a variable. The backtick syntax, altough it works, is legacy syntax.
  • \s: whitespace
  • \S: anything but whitespace. I used that since .* is greedy and would include everything after value="SOMETHING" autocomplete="off
Nic3500
  • 8,144
  • 10
  • 29
  • 40