Finding hidden field values in a web page before submitting a login form
Whenever chaining awk together with grep, sed, head, tail or other text processor tools, consider the possibility of doing it all with just awk, as it's very powerful. Reducing the number of processes in the pipeline makes sense, and in some examples can drastically improve performance.

Here's one way to write the two lines of pipelines as a single line (expanded to multiple lines for readability):

awk -F '"' '
    /csrf1/ {csrf1 = $4 "=" $6}
    /csrf2/ {
      csrf2 = $4 "=" $6;
      if (count++ &gt; 0) {
        print csrf1 "&amp;" csrf2;
        exit; 
      }
    }' signup.php


That is:


When you see a line matching "csrf1", save it
When you see a line matching "csrf2", save it, and:


If this is the first time, increment the count and do nothing else
If this is the second time, print csrf1 and csrf2 and stop processing



Note that the algorithm overwrites the first csrf1 and csrf2 values that were seen, and print only the second time.

I also used -F '"' instead of -F "\"" because it's easier to both write and read.

And if you want to save the result of this in a variable, then instead of:


CSRF=`awk ...`



Use the modern $(...) syntax:

CSRF=$(awk ...)