Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

awk in UNIX script producing wrong results

Status
Not open for further replies.

pluscarden

Programmer
Jan 26, 2004
26
GB
Hi...

Can anyone help me with the following please...

I have a UNIX script that does something along the following lines...

#!/usr/bin/ksh
...
...
...
...
for i in client_file
do
awk -f myfile.awk -v client=$i report
done
...
...
...

-----------------------------------------------

My problem is that the 'for...done' command works fine when I run it on the command line (I get the expected results from the awk script) but it doesn't work properly when I run the UNIX script itself.

Any ideas?

Thanks in advance

 
P.S.

A bit more info....

The awk script itself is just doing something along the following lines...

BEGIN {
client_found=0
category=0
}

/Category 1/ {category=1}

/Category 2/ {category=2}

/Category 3/ {category=3}

$0 ~ client {
if ( category == 1 ) {
bad[client]=""
client_found=1
}
else if ( category == 2 ) {
bad[client]=""
client_found=1
}
else if ( category == 3 ) {
if ( client_found == 0 ) {
good[client]=""
client_found=1
}
}

END {
if ( client_found == 0 ) {
bad[client]=""
}
for ( b in bad ) {
print b >> "badfile"
}

for (g in good ) {
print g >> "goodfile"
}
}


----------------------------

The report file looks something like this...

Category 1
client1
client2
client3

Category 2
client2
client4

Category 3
client1
client5
client6

------------------------------------------------

and clientfile contains the following...

client1
client2
client3
client4
client5
client6
client7
client8

-------------------------------------------------

Expected result is that "badfile" should end up containing the following :

client1
client2
client3
client4
client7
client8

and that "goodfile" should end up contaning the following...

client5
client6

------------------------------------------------------

as I said, this works fine when I run it on the ommand line. But when I run it via the UNIX script, it just puts every single client into the "badfile" and oesn't even bother creating a "goodfile"

 
> for i in client_file
Have you tried this:
Code:
for i in $(<client_file)

Hope This Help
PH.
 
You read the report file for each client.
The following awk program, proceed all clients in a single awk program execution.

awk -f my_file.awk report client_file

[tt]
#
# GoodBad - Memorize clients from report file
# Args: report_file
# Result: Good[] = Clients from category 3
# Bad[] = Other clients
#

function GoodBad (report_file, good_category) {
good_category = 0;
while (getline < report_file) {
if ($0 ~ /^Category /) {
good_category = ($2 == 3);
} else {
if (good_category && $1 != &quot;&quot;)
Good[$0] = &quot;&quot;;
else
Bad[$0] = &quot;&quot;;
}
}
}

#
# Initialisations - Read report_file
#

BEGIN {
GoodBad(ARGV[1]);
ARGV[1] = &quot;&quot;;
if (ARGC == 2) ARGV[ARGC++] = &quot;-&quot;;
}

#
# Read client file -
# Separate the good clients from bad
#

{
if ($0 in Bad || ! ($0 in Good))
print $0 >> &quot;badfile&quot;;
else
print $0 >> &quot;goodfile&quot;;
}
[/tt]

Jean Pierre.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top