1

I have been looking through an old script I wrote which writes whois output to a file and then grep for certain info in that file.

I thought maybe I should change this and save the whois output as a variable - but when I do that, the output loses its formatting.

me@server:~$ echo $info_domain
 Whois Server Version 2.0 Domain names in the .com and .net domains
 can now be registered with many different
 competing registrars. Go to http://www.internic.net for detailed
 information. Server Name: EXAMPLE.COM.AU Registrar: ENETICA PTY LTD
 Whois Server: whois.enetica.com.au Referral URL:
 http://www.enetica.com.au Server Name: EXAMPLE.COM.FLORAMEIYUKWONG.COM
 IP Address: 173.203.204.123 Registrar: GODADDY.COM, LLC Whois Server:
 whois.godaddy.com Referral URL: http://registrar.godaddy.com Server
 Name: EXAMPLE.COM.RAFAELYALUFF.COM IP Address: 173.203.204.123
 [...]

Desired Output:

me@server:~$ whois example.com

Whois Server Version 2.0

Domain names in the .com and .net domains can now be registered with many different
competing registrars. Go to http://www.internic.net for detailed information.

   Server Name: EXAMPLE.COM.AU    Registrar: ENETICA PTY LTD    Whois Server: whois.enetica.com.au    Referral URL: http://www.enetica.com.au

   Server Name: EXAMPLE.COM.FLORAMEIYUKWONG.COM    IP Address:
173.203.204.123    Registrar: GODADDY.COM, LLC    Whois Server: whois.godaddy.com    Referral URL: http://registrar.godaddy.com

   Server Name: EXAMPLE.COM.RAFAELYALUFF.COM    IP Address:
173.203.204.123    Registrar: EXAMPLE.COM, LLC    Whois Server: whois.domain.com    Referral URL: http://www.example.com

.....

......

How can I save whois output to a variable without it losing its formatting?

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
bsmoo
  • 949
  • 4
  • 10
  • 23
  • Take a look at IFS, it may be related to this and you be able to fix it using IFS='\n'; – V H Oct 03 '14 at 10:36
  • Maybe instead of saving the whois output to a variable, you can save it to a *file* and then grep the file for what you are interested in: `whois example.com > whois.out; grep -E 'inetnum|NetRange' whois.out`. – Jens Oct 03 '14 at 11:48

1 Answers1

2

This might be a quoting issue. Instead of

echo $info_domain

try

echo "$info_domain"

so that the format is preserved. Technically, the double quotes prevent what shell gurus call word splitting.

Jens
  • 69,818
  • 15
  • 125
  • 179
  • However... $ grep -E 'inetnum|NetRange' "$info_domain" Is returning a strange output.. – bsmoo Oct 03 '14 at 10:50
  • 2
    @ubuntu101010101 Yes, grep treats the info_domain contents as a *file name*. You need to pass the data on stdin with `echo "$info_domain" | grep -E 'inetnum|NetRange'` – Jens Oct 03 '14 at 10:51