Script not working as desired

Reborg,

Sorry to bother you. I have tried the code you suggested and it's not creating new files after they satisfy the criteria. If any of the files don't satisfy the criteria it should not create the files at all. Please see my output below.

Satya,

I see that in your script you are using ARGIND and ARGC which are specific to nawk and gawk languages and it will not work for you if they are not installed on your system. Other than that I think Reborg should help you with the code he wrote.

Thanks

Thanks Sravan.

Reborg,

I don't have either nawk or gawk installed and hence the script is not working as desired. Is think there is a way to solve this as we got the incorrect output before? Kindly suggest.

I am just getting the name of the files when I run the script. So please help.

Code:

Can you post the output of

uname -a again, I think it was HPUX, but am not certain.

Reborg,

Here is the output of uname -a

HP-UX drwatson B.11.00 B 9000/800 615399343 16-user license

That's what I though, HPUX replaced both awk and nawk with a new version of awk in 11.

This may work, but I don't have a HPUX machine, and don't know exactly how HPUX awk will behave.

awk '{data[NR] = $0; out=FILENAME "new"; file[NR]=out; records[out]=NR } END { for ( r in records ) { sum += data[records[r]]; print sum } print ARGC, NR, NR - (--ARGC * 2);  if ( sum != NR - (ARGC * 2) ) { print "error" } else {for ( i=2; i < NR; i++ ) { print data > file } close(file)}}' $files

Reborg,

I really salute your skills. Let me give it a try. I will revert back with results.

Reborg,

It all most seems working. Except for the fact that in one file header is still there and in another file the count of records at the bottom is present. If both of them can be removed I think you made it all. Please see below for the debug script and output.

code with some numbers thrown out:

Ouput files:

Satya,

I am trying your script and getting the same output as you are getting. Let me know if you get to correct answer.

Reborg,

Please help dude!!! :confused:

awk 'FNR != 1 {

data[NR] = $0
out=FILENAME "new"
file[NR]=out
records[out]=NR

}



END {
        for ( r in records ) {
                sum += data[records[r]]
                print sum
        }

        print ARGC, NR, NR - (--ARGC * 2)
        if ( sum != NR - (ARGC * 2) ) {
                print "error"
        }
        else {
                for ( i in file ) {
                        if ( i != records[file] ) {
                                print data > file
                        }
                }
                close(file)
        }
}
' $files

Reborg : Simply Awesome!!!! Awesome Dude!!!

It's working perfect now. Great man. I started learning UNIX by this forum a lot.

Can you please explain the logic here if you have time. I am trying to understand by breaking it into pieces.

Reborg:

Just a small question. If at the end of writing data rows into new files for each respective file if I want to append all the data rows into one single file what should i do? This should happen only if all the files have correct counts and records. I am checking where I can use the >> to append all the data rows and create one file for that day namely BARE01_DLY_20060725.

Kindly let me know.

Assuming again you only want to do it if everything validates ok in the individual files:

awk -v complete_file=${1}_$(date+%Y%m%d) 'FNR != 1 {

data[NR] = $0
out=FILENAME "new"
file[NR]=out
records[out]=NR

}



END {
        for ( r in records ) {
                sum += data[records[r]]
        }

        if ( sum != NR - (ARGC * 2) ) {
                print "error"
        }
        else {
                for ( i in file ) {
                        if ( i != records[file] ) {
                                print data > file
                                print data > complete_file
                        }
                }
                close(file)
        }
        close(complete_file)
}
' $files

Reborg,

It's not writing the data to the CARE01_DLY_20060725 file but it created one file with name CARE01_DLY_AUS_20060725_new with one line of data.

Please see output from below:

3
7
3 11 7
awk: A print or getline function must have a file name.
The input line number is 5. The file is CARE01_DLY_MKT_20060725.
The source line number is 24

I've updated the code in Post #14...got rid of the debug, and added the quotes I'd forgotten around the filename.

Reborg,

You are simply great man!!!! :slight_smile:

I just changed the code so that I am not hard coding the name of the final file. Please let me know if this works. I am just including the changed portion here from post#14

Will this work?

I want to just let you know that I call the script like this satya.ksh CARE01_DLY

I just want to let you know that I have become your fan. :cool:

Reborg,

It created the file with name ${1}_${date+%Y%m%d). I should have kept single quotes i guess. Kindly suggest.

#14 updated again.

Reborg,

It created the file CARE01_DLY_ The date portion is missing though. Are we missing any where?