Whois script bash

614 Views Asked by At

I have made whois script in bash:

#!/bin/bash

# Store the list of domains in an array
while read domain
do
  # Output the header
  echo "Domain,Registrar,Creation Date,Expiration Date"

  # Perform a WHOIS lookup for each domain
  whois_output=$(whois "$domain")

  # Extract the relevant information from the WHOIS output
  registrar=$(echo "$whois_output" | grep "Registrar:" | awk '{print $2}')
  creation_date=$(echo "$whois_output" | grep "Creation Date:" | awk '{print $3}')
  expiration_date=$(echo "$whois_output" | grep "Expiration Date:" | awk '{print $3}')

  # Output the information in a row
  echo "$domain,$registrar,$creation_date,$expiration_date" >> domain_info.csv
done < domains.csv

The output is coming in vertical way:

Facebook.com
Creation date
Update date


And I want data in horizontal way like: 


Facebook.com,creation date , update date

this is the output I want & this the output I am getting

2

There are 2 best solutions below

2
Jardel Lucca On BEST ANSWER

For the vertical to horizontal conversion, I think the simplest fix is to simply remove the newlines which can be done by piping to tr command:

# Output the information in a row
echo "$domain,$registrar,$creation_date,$expiration_date" | tr -d '\n' >> domain_info.csv
0
jhnc On

You have assumed that whois_output will contain something after the whois call and that registrar, creation_date and expiration_date will all end up with precisely one value each.

These assumptions are incorrect.

For example, the output of whois facebook.com | grep Registrar: (when I run it here) is:

   Registrar: RegistrarSafe, LLC
Registrar: RegistrarSafe, LLC

so when you run your awk '{print $2}' command, the variable registrar contains:

RegistrarSafe,
RegistrarSafe,

(There is an embedded newline.).

Later when you do echo "$domain,$registrar,[...]" you will get:

facebook.com,RegistrarSafe,
RegistrarSafe,,[...]

The fix to your problem is to ensure precisely one value is assigned to each of your variables, and that they do not contain embedded newlines.

For valid csv output, you should probably also quote any value that contains commas.