Ubuntu – Why the bash script only outputs one line on echoing awk variable

awkbashscripts

Anyone can probably tell I'm new at scripting and have tried doing as much homewaork before posting here. I have what I think is a simple script parsing out DNS zone file for IP and unique domain names. I have the results I want with the awk outout though I'm having trouble putting this all in a script to output an expected formated file for input into another system. the output is printing only one line per lookup.

#/bin/bash
addresses=($(grep '<uniquefield>' <filename> | awk -F'[ .]' '{print $5 "." $4 "." $3 "." $2"/"$1}'))
domains=($(grep -v '<uniquefield>' <filename> | awk '<uniquefield> {print $1}'|awk -F'.' 'sub(FS $(NF-4) FS $(NF-3) FS $(NF-2) FS $(NF-1) FS $NF,"")'))

echo ${addresses[1]}
echo ${domains[2]}

for i in "${addresses[@]}"
do
    :
    echo $addresses, disable, , , >import.csv
done
for i in "${domains[@]}"
do
    :
    echo $domains, disable, , , >>import.csv
done

Best Answer

You're not using the $i variable inside the loop

for i in "${addresses[@]}"; do
# ..^
    echo $addresses, disable, , , >import.csv
# .......^^^ should be: echo $i, ...
done

For an array variable, when you print the variable without specifying an index, bash seems to give you just the first element.

$ x=( one two three )
$ echo $x
one
Related Question