I am running a loop like so:
for i in $(cat ips_uniq.txt)
do
whois $i | grep 'netname|country|org-name' | sed ':a;N;$!ba;s/\\n//g'
done
Output:
netname: NETPLUScountry: INcountry: INcountry: IN
netname: NETPLUScountry: INcountry: INcountry: IN
This is good however my ips_uniq.txt contains over 300 uniq IP addresses so Ideally I want the IP address to be on the same line of each output.
CodePudding user response:
My version:
#!/bin/bash
while IFS= read -r i
do
if [[ "$i" != "" ]]
then
results=$(whois "$i" | grep -iE 'netname|country|org-name' | tr '\n' ' ')
echo "$i $results"
fi
done < ips_uniq.txt
- using the
while read
method is a safe way to read files line per line, avoiding all problems with spaces or weird line formats. Read https://mywiki.wooledge.org/BashFAQ/001 for details. Not required in your case, but a good construct to learn about. - the
if
is to avoid empty lines. - then store the result of the
whois | grep
combination in a variable. - note that I replaced your
sed
with a simpletr
which removes the\n
, and adds a space to split fields. Modify as required. - then the final
echo
adds the IP address prefix to the results line.
I tested with ips_uniq.txt equal to
8.8.8.8
74.6.231.20
and the result is
8.8.8.8 NetName: LVLT-ORG-8-8 Country: US NetName: LVLT-GOGL-8-8-8 Country: US
74.6.231.20 NetName: INKTOMI-BLK-6 Country: US
Using printf
you could format the output to be better (i.e. fixed length columns for example).