在列表中显示grep输出

时间:2018-05-15 02:06:16

标签: bash shell tabular

我需要一些帮助来处理从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

除了我试图在这样的表中显示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尝试了很多方法,但我总是弄乱桌子。 我对shell脚本很新,所以如果有人可以帮忙解决这个问题,我将非常感激。

1 个答案:

答案 0 :(得分:0)

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

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

而不是&#34;创建&#34;我正在&#34;创造&#34;。有时到期或到期。找到最简单的常见数据,获得您所需要的并且不会产生误报。你也可以使用egrep 'EXPIR(Y|E|ATION)[^:]*:'或其他一些;扩展的正则表达式是你的朋友。

使用<{p}}的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

我把主机var拿出来;把你的东西放回去。 干杯。 :)