Arun
Arun

Reputation: 1220

Shell : Add input lines subset to prefix dig output

At present,I get NS(Name Server) record for a list of domains in a loop fashion.

 #ns_check.sh
 for i in $(cat Input.txt);do dig +short NS $i;done

Input file has only domains.

#Input.txt
example.com
hello.com
blah.com

I get output as,

 #Out.txt
    ns4.p05.dynect.net.
    ns2.p05.dynect.net.
    ns1.p05.dynect.net.
    ns3.p05.dynect.net.
    ns-315.awsdns-39.com.
    ns-564.awsdns-06.net.
    ns-1162.awsdns-17.org.
    ns-1949.awsdns-51.co.uk.
    a3.verisigndns.com.
    u1.verisigndns.com.
    a2.verisigndns.com.
    a1.verisigndns.com.
    u2.verisigndns.com.
    u3.verisigndns.com.

Now, I would like to have my Input.txt as

  #Input.txt
    1,example.com
    2,hello.com
    3,blah.com

Output has to be like,

#Out.txt
        1,ns4.p05.dynect.net.
        1,ns2.p05.dynect.net.
        1,ns1.p05.dynect.net.
        1,ns3.p05.dynect.net.
        2,ns-315.awsdns-39.com.
        2,ns-564.awsdns-06.net.
        2,ns-1162.awsdns-17.org.
        2,ns-1949.awsdns-51.co.uk.
        3,a3.verisigndns.com.
        3,u1.verisigndns.com.
        3,a2.verisigndns.com.
        3,a1.verisigndns.com.
        3,u2.verisigndns.com.
        3,u3.verisigndns.com.

How to add the Input ID to the output of each and every NS record of domain obtained ? New to shell scripting,Any suggestions on how to fix this ?

Upvotes: 1

Views: 348

Answers (1)

anubhava
anubhava

Reputation: 786271

You can use this while loop:

while IFS=, read -r n h; do
    printf "$n,%s\n" $(dig +short NS "$h")
done < input.txt
  • IFS=, read -r n h will read each comma delimited line into 2 variables n (number) and h (host).
  • printf "$n,%s\n" $(dig +short NS "$h") will replace each line start with $n, in the dig command's output.

Upvotes: 1

Related Questions