在列 table 中显示 grep 输出

Display grep output in column table

我需要一些脚本帮助,该脚本从 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

一切正常,除了我试图在 table 中显示我的 grep 命令的结果,如下所示:

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
------------------------------------------

我用 sedawk 尝试了很多方法,但我总是弄得一团糟 table。 我是 shell 脚本的新手,所以如果有人可以帮助解决这个问题,我将不胜感激。

尝试这样的操作...根据您的需要进行编辑。

{ # 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

而不是 "Created" 我得到 "Creation"。有时 Expire 或 Expire 中的 Expiry。找到最简单的通用数据,这些数据可以满足您的需求并且不会产生误报。您也可以使用 egrep 'EXPIR(Y|E|ATION)[^:]*:' 或类似的东西;扩展正则表达式是你的朋友。

使用 domain-file.txt

YAHOO.com
GOOGLE.com

这给了我

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

我把主机变量拿出来了;根据需要把你的放回去。 干杯。 :)