Search code examples
bashshelltabular

Display grep output in column table


I need some help with a script that grabs info from whois to display the creation, expiry date and ns server of specific domain names.

#!/bin/sh
cat domain-file.txt  | while read line ; do
lLine="$(echo $line | tr '[A-Z]' '[a-z]')"
echo "$lLine\t" >> table.csv;
sleep 3
echo "\n$lLine"
host=whois.nic.re
created=$(whois -h $host $lLine | egrep -i 'created:')
echo "$created\t" >> table.csv
sleep 2
expire=$(whois -h $host $lLine | egrep -i 'Expiry Date:')
echo "$expire\t" >> table.csv
sleep 2
nserver=$(whois -h $host $lLine | egrep -i 'nserver:')
echo "$nserver\t" >> table.csv
echo "------------------------------------------" >> table.csv
done
exit

Everything is working well except that I'm trying to display the result of my grep command in a table like this:

Domain          Created Date    Expiry date     NS
abcd.com        19/01/2018      19/01/2019      ns.abcd.com ns2.abcd.com
1234.com        19/01/2018      19/01/2019      ns.1234.com ns2.1234.com

Instead I'm getting an output like that:

abcd.com
Created date: 19/01/2018
Expiry date: 19/01/2019
nserver: ns.abcd.com
nserver: ns2.abcd.com
------------------------------------------
1234.com
Created date: 19/01/2018
Expiry date: 19/01/2019
nserver: ns.1234.com
nserver: ns2.1234.com
------------------------------------------

I have tried many ways with sed and awk but I always get messy table. I'm pretty new with shell script so If anyone can help with this it would be much appreciated.


Solution

  • Try something like this... Edit for your needs.

    { # opening a brace to collect and control I/O
      typeset -l line created expire ns                  # preset to lowercase
      # controlled width header
      printf "%-20s%-25s%-25sNS\n" Domain "Create Date" "Expiry Date"
    
      while read line                                    # downcases as it loads
      do # only read each domain once. Save and reuse.
         typeset -u data=$(whois $line)                  # this one upcases
         # %-14s is a 14 wide left-adjusted string
         printf "%-20s" "$line"
         # use a regex to account for variation in server output
         created=$( echo "$data" | egrep 'CREAT[^:]*:' ) # no -i needed
         printf "%-25s" "${created#*: }"                 # autotrim labels
         expire=$( echo "$data" | egrep 'EXPIR[^:]*:' )  # regex again
         printf "%-25s" "${expire#*: }"                  # autotrim
         echo "$data" | egrep 'N[^:]*SERVER:' |          # outputs multiple
           while read ns                                 # loop them
           do printf "${ns#*: } "                        # to easily autotrim
           done
         printf "\n"                                     # and end the line
      done
    # now close the brace and define your stdin & stdout
    } < domain-file.txt > table.csv
    
    exit
    

    Instead of "Created" I was getting "Creation". Sometimes Expiry in Expire or expiration. Find the simplest common data that gets what you need and doesn't give false positives. You could also use egrep 'EXPIR(Y|E|ATION)[^:]*:' or some such; extended regexes are your friend.

    Using domain-file.txt of

    YAHOO.com
    GOOGLE.com
    

    this gives me

    Domain              Create Date              Expiry Date              NS
    yahoo.com           1995-01-18t05:00:00z     2023-01-19t05:00:00z     ns1.yahoo.com ns2.yahoo.com ns3.yahoo.com ns4.yahoo.com ns5.yahoo.com
    google.com          1997-09-15t04:00:00z     2020-09-14t04:00:00z     ns1.google.com ns2.google.com ns3.google.com ns4.google.com
    

    I took the host var out; put yours back in as required. Cheers. :)