[ACCEPTED]-parse http response header from wget-wget
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"
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).
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.
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)
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
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.