Home > OS >  Parsing multiple line output into single cell within CSV file
Parsing multiple line output into single cell within CSV file

Time:10-13

This is my code.

#!/bin/bash

for domain in google.com twitter.com
do
        ns_record=$(dig NS  short $domain)
        echo "$domain" "," "$ns_record" >> output.csv
done

It generates an output which is mentioned in below pic.

enter image description here

I want output in the below format.

enter image description here

What should be my code?

CodePudding user response:

Insert a comma after each newline in ns_record:

echo "$domain,${ns_record//$'\n'/$'\n',}"

CodePudding user response:

You can save the ns_record output as an array and then loop over the array outputting at a column position of your choosing. You will have to make a special case for the first record output at the end of $domain, but that is all simple to do with printf.

For example, you can modify your script to do:

#!/bin/bash

declare -i col2pos=20     ## set column 2 position

for domain in google.com twitter.com; do
  ns_record=($(dig NS  short $domain))    ## create array of dig output
  
  nsspaces=$(( col2pos - ${#domain} ))    ## get no. of chars in domain
  
  ## output domain followd by nsspaces spaces and first element of ns_record
  printf "%s%*s%s\n" "$domain" "$nsspaces" " " "${ns_record[0]}"
  
  ## loop outputting remaining ns_record at col2pos
  for ((i=1; i< ${#ns_record[@]}; i  )); do
    printf "%*s%s\n" "$col2pos" " " "${ns_record[i]}"
  done
  
done

(you can set $col2pos as you like)

Example Use/Output

$ bash ns_record.sh
google.com          ns2.google.com.
                    ns1.google.com.
                    ns3.google.com.
                    ns4.google.com.
twitter.com         a.r06.twtrdns.net.
                    d.r06.twtrdns.net.
                    ns4.p34.dynect.net.
                    ns1.p34.dynect.net.
                    d01-01.ns.twtrdns.net.
                    c.r06.twtrdns.net.
                    d01-02.ns.twtrdns.net.
                    b.r06.twtrdns.net.
                    ns2.p34.dynect.net.
                    ns3.p34.dynect.net.

Sorted ns_record

You may want the output of the ns_record information to appear sorted. You can easily do that piping the output of dig to sort, e.g.

  ns_record=($(dig NS  short $domain | sort)) ## create array and sort

Sorted Output

Now your output appears as:

$ bash ns_record.sh
google.com          ns1.google.com.
                    ns2.google.com.
                    ns3.google.com.
                    ns4.google.com.
twitter.com         a.r06.twtrdns.net.
                    b.r06.twtrdns.net.
                    c.r06.twtrdns.net.
                    d.r06.twtrdns.net.
                    d01-01.ns.twtrdns.net.
                    d01-02.ns.twtrdns.net.
                    ns1.p34.dynect.net.
                    ns2.p34.dynect.net.
                    ns3.p34.dynect.net.
                    ns4.p34.dynect.net.

I find that a bit easier to look through.

Let me know if you have questions.

  • Related