如何将whois查询结果的grep输出整理为规范表格格式?
I wrote a shell script that reads domains from domain-file.txt, queries their creation date, expiry date, and NS servers via the whois.nic.re server, and writes the results to table.csv. The script works, but the grep output is formatted incorrectly.
Desired Output:
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
Current Output:
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 ------------------------------------------
Original Script:
#!/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
Hey there! Let's fix this formatting issue and optimize your script a bit—right now you're making three separate whois calls per domain, which is slow and unnecessary. Here's how to get the clean table output you want:
Revised Script
#!/bin/sh # Set output file and write header first OUTPUT_FILE="table.csv" echo -e "Domain\tCreated Date\tExpiry date\tNS" > "$OUTPUT_FILE" # Read each domain from the file (handles spaces and avoids parsing issues) while IFS= read -r line; do # Convert domain to lowercase domain=$(echo "$line" | tr '[A-Z]' '[a-z]') echo "Processing $domain..." # Call whois ONCE and store the full response (saves time and server load) whois_data=$(whois -h whois.nic.re "$domain") # Extract creation date, stripping the "Created date:" prefix created_date=$(echo "$whois_data" | grep -i 'created:' | sed 's/^[Cc]reated: //') # Extract expiry date, stripping the "Expiry Date:" prefix expiry_date=$(echo "$whois_data" | grep -i 'expiry date:' | sed 's/^[Ee]xpiry [Dd]ate: //') # Extract all NS servers, strip prefixes, and join into a single space-separated string ns_servers=$(echo "$whois_data" | grep -i 'nserver:' | sed 's/^[Nn]server: //' | tr '\n' ' ') # Trim any trailing whitespace from the NS list ns_servers=$(echo "$ns_servers" | sed 's/ *$//') # Write the formatted line to the output file echo -e "$domain\t$created_date\t$expiry_date\t$ns_servers" >> "$OUTPUT_FILE" # Add a delay to avoid hitting whois server limits (adjust as needed) sleep 3 done < "domain-file.txt" echo "Done! Results saved to $OUTPUT_FILE"
Key Improvements Explained
- Single
whoisCall: We fetch all data for a domain in one go, which speeds up the script and reduces unnecessary load on the whois server. - Structured Header: We start by writing a clear table header to
table.csvso your output is organized from the start. - Clean Field Parsing:
sedstrips away the label text (likeCreated date:) to leave only the raw data you need.- For NS servers,
tr '\n' ' 'converts multiple lines of entries into a single space-separated string.
- Robust Domain Reading:
while IFS= read -r linehandles edge cases (like domains with spaces, though rare here) and avoids unexpected parsing errors. - Simplified Delays: A single
sleepper domain keeps the script respectful to the whois server without adding unnecessary waits.
Testing the Script
Make sure domain-file.txt has one domain per line, then run the script. You can view the clean output in your terminal with:
cat table.csv | column -t
Or import table.csv directly into a spreadsheet tool like Excel or Google Sheets.
内容的提问来源于stack exchange,提问作者Marcus

