[ACCEPTED]-parse http response header from wget-wget

Accepted answer
Score: 20

The output of wget you are looking for is 1 written on stderr. You must redirect it:

$ wget -SO- -T 1 -t 1 http://myurl.com:15000/myhtml.html 2>&1 | egrep -i "302" 
Score: 10

wget prints the headers to stderr, not to stdout. You 4 can redirect stderr to stdout as follows:

wget -SO- -T 1 -t 1 http://myurl.com:15000/myhtml.html 2>&1 | egrep -i "302"

The 3 "2>&1" part says to redirect ('>') file 2 descriptor 2 (stderr) to file descriptor 1 1 (stdout).

Score: 2

A bit enhanced version of already provided 10 solution

wget -SO- -T 1 -t 1 http://myurl.com:15000/myhtml.html 2>&1 9 >/dev/null | grep -c 302

2>&1 >/dev/null will trim off 8 unneeded output. This way egrep will parse 7 only wget`s stderr, what eliminates possibility 6 to catch strings containing 302 from stdout 5 (where html file itself outputted + download 4 proces bar with resulting bytes count e.t.c.) :)

egrep -c counts 3 number of matched strings instead of simply 2 output them. Enough to know how much strings 1 egrep matched.

Score: 2

wget --server-response http://www.amazon.de/xyz 2>&1 | awk 1 '/^ HTTP/{print $2}'

Score: 1

Just to explicate a bit. The -S switch in 5 the original question is shorthand for --server-response.

Also, I 4 know the OP specified wget, but curl is similar 3 and defaults to STDOUT .

curl --head --silent $yourURL

or

curl -I -s $yourURL

The --silent switch is 2 only needed for grep-ability: (-s turns off progress 1 % meter)

Score: 0

I found this question trying to scrape response 6 codes to large lists of URLs after finding 5 curl very slow (5+s per request).

Previously, I 4 was using this:

curl -o /dev/null -I --silent --head --write-out %{http_code} https://example.com

Building off Piotr and Adam's 3 answers, I came up with this:

wget -Sq -T 1 -t 1 --no-check-certificate --spider https://example.com 2>&1 | egrep 'HTTP/1.1 ' | cut -d ' ' -f 4

This has a 2 few bugs e.g. redirects return 302 200, but overall 1 is greased lightning in comparison.

More Related questions