Shell-Script
在表格列中顯示 grep 輸出
我需要在表格列中顯示 grep 輸出。
我有一個腳本,可以從 whois 獲取資訊以顯示特定域名的創建、到期日期和 ns 伺服器。
#!/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
除了我試圖在這樣的表格中顯示輸出之外,一切都執行良好:
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
相反,我得到了這樣的輸出:
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
我總是使用逗號作為 CSV 文件的分隔符,以便在 opencalc 或 excel 中更好地查看。試試下面我沒有測試過的程式碼。
#!/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
在終端上查看結果:
column -t -s',' table.csv