Redirecting to different output files with awk.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Redirecting to different output files with awk.
# 1  
Old 03-02-2010
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.

Code:
#!/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.
# 2  
Old 03-02-2010
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
# 3  
Old 03-02-2010
I'll try that. Thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Redirecting output using if with awk

I have this line were I am selecting some fields from one file and creating a new file for the selected data. awk -F "\"*,\"*" '{print $1"," $2}' folderone/list.txt > folderone/qlist.txt This works, but then from this new file I want it to create a new file where it separates data: $2 >5 it... (2 Replies)
Discussion started by: rluna
2 Replies

2. Shell Programming and Scripting

Redirecting the results to different output files

Hi All, I am trying a shell script and need your help on taking the results to different output files. I have tried the below code: nawk ' {CNF = (length()-10)/7 printf "%9s", substr ($0, 1, 9) for (i=0; i<=CNF; i++) T = substr ($0, 10+i*7, 7) TMP = 100 - (T + T + T + T + T + T + T + T... (24 Replies)
Discussion started by: am24
24 Replies

3. Shell Programming and Scripting

Awk: Print Error While Redirecting output in multiple Files

Hi, I have a following code in which I am unable to redirect to multiple files. Can anybody please help with some corrections awk -F, '{ if ( substr($1,26,2)=="02" && substr($1,184,14)=="MTSCC_VALFIRST") { array1++ array2++ array3++ } else if (substr($1,26,2)=="03" &&... (4 Replies)
Discussion started by: siramitsharma
4 Replies

4. Post Here to Contact Site Administrators and Moderators

Redirecting grep output to multiple files

Hi All, I am trying to redirect the grep output to multiple files, can you please help with that. Below is the command im using to match my pattern grep \<proxyType\>$PxyType $DIR/EndureFiles.json > File_Name*.json Note : $DIR and $PxyType is already defined in my script Im able... (0 Replies)
Discussion started by: Deena1984
0 Replies

5. Shell Programming and Scripting

redirecting output using if-then-else in awk

I am trying to filter records based on number of "|", delimiter in my application. If number of "|" is greater than 14 its a bad record, else its a good record. I have to redirect output to two different files based on the if-then-else evaluation in AWK. if number of “|” in file_0 > 14 ... (2 Replies)
Discussion started by: siteregsam
2 Replies

6. Shell Programming and Scripting

Redirecting echo output to 2 flat files

Hello all, I have the following 2 questions.. 1) I would like to capture the output of an echo command to 2 different files at the same time. This does not seem to work. Any ideas? echo ==== started on `date` ==== >> res1.log res2.log 2) Would it be possible to just get the 5th... (2 Replies)
Discussion started by: luft
2 Replies

7. UNIX for Dummies Questions & Answers

awk and redirecting to files

Hello, I have a set of data (comma seperated) that I want to save to multiple files according to one of the fields in the data set. I can easily do this with the following script: BEGIN { FS = OFS = ","} NF {print $0 >> ($2 "" ".csv")} It works perfectly on a set of dummy data I have set... (8 Replies)
Discussion started by: pfft
8 Replies

8. Shell Programming and Scripting

Confused about redirecting output from awk.

I have a simple script written in awk whose purpose is to go through some php files and replace some strings. Naturally, I want the changes to be written out to the files. The script looks generally as follows: { gsub(/'replacethis'/, "with this"); # a bunch of these print $0 > FILENAME }... (3 Replies)
Discussion started by: face1
3 Replies

9. Programming

execl() + redirecting output to text files

Im currently using execl() to run the ls command and redirect the output to a text file. Unfortunately, when I check the text file, no information has been written to it. I am calling execl() with the ls command like this execl( "/bin/ls" , "-al" , '>' , "dirlist.txt" ,(char *) 0 ); ... (5 Replies)
Discussion started by: JamesGoh
5 Replies

10. UNIX for Dummies Questions & Answers

Redirecting output to multiple log files?

If I wanted to redirect output to multiple log files, what would be the best way to do that? echo "Unix is awesome" >>unixgod.log >>unixgod.log (3 Replies)
Discussion started by: darthur
3 Replies
Login or Register to Ask a Question