0

I need to display grep output in table columns.

I have a script that grab 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 im trying to display the output 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 im 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
1

I always use comma as a delimiter for CSV files for a better view in opencalc or excel. Try the following code which I have not tested.

#!/bin/sh
echo -e "Domain,Created Date,Expiry_date,NS" > table.csv
cat domain-file.txt  | while read line ; 
do
    lLine="$(echo $line | tr '[A-Z]' '[a-z]')"
    host=whois.nic.re
    created=$(whois -h $host $lLine | egrep -i 'created:' | awk -F ':' '{print $NF}')
    expire=$(whois -h $host $lLine | egrep -i 'Expiry Date:' | awk -F ':' '{print $NF}')
    nserver=$(whois -h $host $lLine | egrep -i 'nserver:' | awk -F ':' '{print $NF}')
    echo -e "$lLine,$created,$expire,$nserver" >> table.csv
done
exit

To view result on terminal:

 column -t -s',' table.csv
1

As you are saying that , you are able to save information in csv with delimiter TAB i.e. \t and goal of asking this problem is to pretty print the data in columns

Just try this, and it will work

column  -t -s$'\t' -n table.csv

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.