Redirecting to different output files with awk.

Well, it didn't take me long to get stumped again. I assure you that I'm not mentally deficient, just new to scripting.

So, here's the gist. I want to redirect output from awk based off of which branch of an if-else statement under which it falls.

#!/bin/bash

#some variables
CURPATH=`dirname $0`
CSV=$CURPATH/output.csv
HNS=$CURPATH/standard_hosts.txt
GROUPS=$CURPATH/groups.csv
DOMAINS=$CURPATH/domains.txt


if [ -f $CSV ];
then
        rm $CSV
        echo "Deleted old CSV"
fi

if [ -f $HNS ];
then
        rm $HNS
        echo "Deleted old standard_hosts.txt"
fi


#Convert useless HTM file into a csv file

tr -d '\t' < $1 | sed 's/<.*>//g' | sed '/^$/d' | tr '\n' ',' | perl -pe 's/,Entity/\nEntity/g' | sed 's/Entity name: //g' > $CSV


#Go through each line, and determine if it's a host, a subnet, a group, or a domain object. Yes, I'm sure there are easier ways to do this, but I'm after effective, not elegant.

#First, we grab all of the host objects.

cat  $CSV | egrep -v "Network Entity" | head | gawk -F, '

        {for (i=1;i<=NF;i++)
                {if (($i == "Network address") && ($(i+1) ~ /[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/))
                        {
                          {printf $1 >> $HNS};
                          {printf "," $(i+1)};
                          {NM = 0};
                          {for (j=1; j<=NF; j++)

                                  {if (($j == "Netmask") && ($(j+1)  ~ /[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/))
                                      {
                                          {print "," $(j+1) >> $HNS;}
                                          {NM = 1};
                                      }

                                  };

                        }
                        {if (NM == 0) {print ",255.255.255.255" >> $HNS}};

                        }
                else
                      {if (($i == "Network address") && ($(i+1) !~ /[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/))
                          {
                               {print "object network " $1 >> $DOMAINS; }

                               {print system("./dig.awk $1" $(i+1)) >> $DOMAINS ;}
                               {print "255.255.255.255" >> $DOMAINS;}

                          }

                      }

                }
        }'



Whereas, when I read the code, I think it'll take stuff that is a straight up hostname / netmask pair and put them in one file (denoted by $HNS), while taking everything that is a domain name and putting them another file (denoted by $DOMAINS), what ends up happening is it creates a bunch of files based off of the contents of $1, which is not what I'm after.

Any ideas? I'm sure it's something simple that I'm doing incorrectly. Google searches have led me to all sorts of stuff about redirecting output for the entire program, but not for individual code branches.

the problem is that awk knows nothing about your shell variables, you need to set them explicitly

try putting this right after the closing '

HNS=$HNS DOMAINS=$DOMAINS

here's a reference --> The GNU Awk User's Guide

Chris

I'll try that. Thanks!