0

I have the below script for whois lookup

for line in $(cat ips.txt)
do
echo $line
whois $line | grep OrgName | awk '{print $2,$NF}' 
done

I am having the output

192.168.1.1
Internet Authority

How can I achieve the output in the below format ?

192.168.1.2 : Internet Authority

DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=20.04
DISTRIB_CODENAME=focal
DISTRIB_DESCRIPTION="Ubuntu 20.04 LTS"

Joel Deleep
  • 1,308
  • 2
  • 14
  • 36
  • I don't get that behavior. What OS are you using? Maybe something like `whois $line | grep OrgName | awk -v LINE=$line '{print $2,LINE}'` is what you want though? – user3783243 Jul 13 '20 at 02:53
  • @user3783243 Your answer worked for me , but why does it provide an output like that as i mentioned in the question , its not the intended output if im not wrong – Joel Deleep Jul 13 '20 at 03:05
  • I don't get that output on a mac.. Not sure in what scenario that output would occur. I can post my solution but that won't answer the question of why it is behaving as it is. You'll likely need to provide the OS and version for users to assist with that question. – user3783243 Jul 13 '20 at 03:12
  • Perhaps you have some unprintable characters (such as `\r`) in ips.txt. Can you check it with `od` or `hexdump` ? – M. Nejat Aydin Jul 13 '20 at 03:17
  • @M.NejatAydin , there is nor carriage return , i double checked it using hexdump . The output seem unintended , I cant understand the reason for that unusual behaviour – Joel Deleep Jul 13 '20 at 03:25
  • @JoelDeleep Neither can I. I don't get that output on a openSUSE Linux. – M. Nejat Aydin Jul 13 '20 at 03:40
  • @M.NejatAydin I got the issue , even though I posted the correct code, back in my server I had a typo. – Joel Deleep Jul 13 '20 at 06:17
  • `echo $line` writes it with a return, so you get 2 lines then with the subsequent whois call. If you do not want the return, do `echo -n $line`. You will then probably need a trailing whitespace for readability – Patrick Mevzek Jul 14 '20 at 17:46
  • You should use RIRs RDAP servers instead of whois. Far easier to parse correctly. – Patrick Mevzek Jul 14 '20 at 17:47

1 Answers1

1

On the 'echo $line' line, the shell was asked to print the value of $line. The shell says ok - done. Then the shell moves on to the next line, that basically says 'get string then pipe it to some string manipulation and print result'.

I believe 'print something on the screen' was asked from the shell twice, 1 by echo 2 by awk, which are from 2 separate lines , so the shell behaved as expected.

To prevent this you can contain the second line in $(), so that echo will print "$line + $(whatever comes out here)"

for line in $(cat ips.txt)
do
echo $line : $(whois $line | grep OrgName | awk '{print $2,$NF}')
done
ZYX Rhythm
  • 73
  • 7
  • Great answer! Reading lines with `for i in $(..)` is an [antipattern in bash](https://mywiki.wooledge.org/DontReadLinesWithFor). A `while IFS= read -r line` loop is preferred. [bashfaq how to read a file line by line](https://mywiki.wooledge.org/BashFAQ/001) – KamilCuk Jul 26 '20 at 08:12