Consider using %{url_effective} in the -w output fields:
awk -v OFS== -v out=/dev/null \
'!/^(#|[[:blank:]]*$)/{print "url", $0; print "out", out}' urls.txt |
curl -K- -sSLI -w '%{num_redirects},%{url_effective},%{http_code}\n' >> output.csv
- Because
-L is used to follow redirects, there may be some discrepencies between the urls from urls.txt and %{url_effective}. If the first url gives a 3xx response, the first column from the output above will be greater than zero, the second column will contain last url followed (not the initial url from urls.txt)
- Above, instead of looping over the file and calling
curl on each line, awk is used to turn urls.txt into a "config" file for curl which is then piped to curl. The formatting for this is specific to curl – for more info, search for -K in man 1 curl
e.g.
$ cat urls.txt
https://unix.stackexchange.com/
#www.example.com
https://unix.stackexchange.com/question
http://unix.stackexchange.com/
$ awk -v OFS== '!/^(#|[[:blank:]]*$)/{print "url",$0; print "out","/dev/null"}' \
urls.txt | curl -K- -sSLI -w '%{num_redirects},%{url_effective},%{http_code}\n'
0,https://unix.stackexchange.com/,200
0,https://unix.stackexchange.com/question,404
1,https://unix.stackexchange.com/,200