Help in getline and system use in gawk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help in getline and system use in gawk
# 1  
Old 06-30-2012
Help in getline and system use in gawk

Hi- I am not getting any output in below code using gawk (I have to do it in Linux). Important to mention is its working fine in Solaris (nawk) environment. Thanks.

Code:
export buff=temp_orders
export cFILE=correct_orders.$pid
export bFILE=bad_orders.$pid

[[ -f $buff ]] && \rm -f $buff
[[ -f $bFILE ]] && \rm -f $bFILE
[[ -f $cFILE ]] && \rm -f $cFILE

     gawk -v buff=$buff -v cFILE=$cFILE -v bFILE=$bFILE '
           BEGIN {
                  FS = "" ;
                  RS = "\n";
                  NR_RECORDS=0;
                  NR_RECORDS_P0301=0;
                  NR_RECORDS_P0381=0;
                  NR_BAD_RECORDS=0;
                  TOT_ORD_LINE=1;
                  RECORD_TYPE="";
             }
             {
                RECORD_TYPE=substr($0, 1, 5 );

                if ( RECORD_TYPE == "P0301" )
                {
                  if ( TOT_ORD_LINE == NR_RECORDS_P0381 )
                   {
                     print ORD_HEAD_FULL >> cFILE;
                     while ((getline line <buff) > 0 ) { print line >> cFILE}
                     rm -f buff;
                   }
                   else
                   {
                     if ( NR_RECORDS > 0 )
                     {
                        print ORD_HEAD_FULL >> bFILE;
                        while ((getline line <buff) > 0 ) { print line >> bFILE}
                        rm -f buff;
                     }
                   }

                   TOT_ORD_LINE=substr($0,31,4);
                   TOT_ORD_LINE=TOT_ORD_LINE+0;

                   ORD_HEAD_FULL=$0;

                   ++NR_RECORDS;
                   NR_RECORDS_P0381=0;
                   NR_RECORDS_P0301=1;
                }
                else
                {
                  if ( RECORD_TYPE == "P0381" )
                  {
                    ++NR_RECORDS_P0381;
                    ++NR_RECORDS;
                    print $0 >> buff;
                  }
                  else
                  {
                      print $0 >> bFILE;
                      ++NR_BAD_RECORDS;
                      ++NR_RECORDS;
                  }
                }
             }

           END   {
                   if ( TOT_ORD_LINE == NR_RECORDS_P0381 )
                   {
                     print ORD_HEAD_FULL >> cFILE;
                     while ((getline line <buff) > 0 ) { print line >> cFILE}
                     rm -f buff;
                   }
                   else
                   {
                     if ( NR_RECORDS > 0 )
                     {
                        print ORD_HEAD_FULL >> bFILE;
                        while ((getline line <buff) > 0 ) { print line >> bFILE}
                        rm -f buff;
                     }
                   }
                 }
           ' $FILE

if [ $status != 0 ]
 then
     echo "P03 Format Check Failed"
 else
     cp $cFILE $FILE
 fi

# 2  
Old 06-30-2012
- Where does the variable "$status" get its value?
- gawk has some quirks and sometimes it needs the --posix command line option

Otherwise could you post input and output samples?
# 3  
Old 06-30-2012
Thanks for quick reply..!!!!

status is just a variable to get the status and print. Nothing significant. Please ignore that part.

INPUT FILE:
Code:
P0301DE15540010041237     RBNN0001000000
P0381DE15540010041237     00999999                                000001N000000
P0301DE229300100176       RCNN0001000001
P0381DE229300100176       90444463                    100699      000001N000001
P0301DE1742001  589D1     VCNN0007000000
P0381DE1742001  589D1     91165829                    4400469     000001N000000
P0381DE1742001  589D1     91167203                    4414169     000001N000000
P0381DE1742001  589D1     93857824                    4430761     000001N000000
P0381DE1742001  589D1     93160220                    4410327     000001N000000
P0381DE1742001  589D1     13247083                    1320364     000001N000000

CORRECT OUTPUT with nawk in Unix:
1. correct Order lines:
Code:
P0301DE15540010041237     RBNN0001000000
P0381DE15540010041237     00999999                                000001N000000
P0301DE229300100176       RCNN0001000001
P0381DE229300100176       90444463                    100699      000001N000001

2. Bad Order Lines (below line should come in error because only 5 P0381 lines are present while as per P0301 header records it is expecting 7 records):
Code:
P0301DE1742001  589D1     VCNN0007000000
P0381DE1742001  589D1     91165829                    4400469     000001N000000
P0381DE1742001  589D1     91167203                    4414169     000001N000000
P0381DE1742001  589D1     93857824                    4430761     000001N000000
P0381DE1742001  589D1     93160220                    4410327     000001N000000
P0381DE1742001  589D1     13247083                    1320364     000001N000000

Wrong OUTPUT with gawk in Linux :
1. correct Order lines(only P0301 type records):
Code:
P0301DE15540010041237     RBNN0001000000
P0301DE229300100176       RCNN0001000001

2. Bad Order Lines (only P0301 type records):
Code:
P0301DE1742001  589D1     VCNN0007000000

---------- Post updated at 03:35 PM ---------- Previous update was at 11:46 AM ----------

Hi,
Seems below file is not getting any values.. Not sure??
buff

Last edited by Scrutinizer; 06-30-2012 at 05:42 AM.. Reason: code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

getline() and fclose()

Hi All, When I do man getline on my Linux system (in BASH), I see an example C code where a file is being read using getline() function. Now, my question is why there is no fclose() to close the open file pointer? People who don't have a Linux system, this is the code that I am referring... (3 Replies)
Discussion started by: shoaibjameel123
3 Replies

2. Shell Programming and Scripting

getline from another file

Here is I want to do: find lines in file filteredtrace.txt which is not continued as a multiply of 4 and strip them from file original_trace_framno. problem is awk used the ' symbol so pipe of getline has to use the " symbol while agument of sed has to use the " symbol, it doesn't... (8 Replies)
Discussion started by: lzq420241
8 Replies

3. Programming

getline()

I can not get 'getline()' to compile. I have tried. string curLine; //= compiler error char* curLine; //=compiler error char curLine; //=compiler error Every example I see uses a string as a getline(); parameter. It does not work for me on Fedora14 with gcc-c++. Thank you so much. This... (1 Reply)
Discussion started by: sepoto
1 Replies

4. UNIX for Dummies Questions & Answers

getline value to pattern

BEGIN { system("clear") blank = " " printf("Please enter your name or emp# : > ") getline < "/dev/tty" printf(">>>>> %s",$1) } /$1/ {.......... I would like for the customer to enter name or... (3 Replies)
Discussion started by: Morph797
3 Replies

5. Shell Programming and Scripting

Using getline in awk

I am using awk and want to use getline from a file like below getline x < file However file consists of two columns and I only want to store $2 Any way I can do this? ---------- Post updated at 06:54 AM ---------- Previous update was at 06:45 AM ---------- Done something like this.... (1 Reply)
Discussion started by: kristinu
1 Replies

6. Shell Programming and Scripting

awk getline

How do you make the getline function return to the original line? The example below should make it clear where I am currently going wrong. Thanks AWK SCRIPT: ------------- awk -F '-' '{ tmpLine = "EMPTY" print "CURRENT LINE :"$0 getline tmpLine print "NEXT LINE :"tmpLine }'... (1 Reply)
Discussion started by: garethsays
1 Replies

7. Shell Programming and Scripting

awk and system getline

Hello, Need some help here. I have this script (test.sh): #!/bin/sh var=$1 (( var = 2 * var )) echo $var Now I want to call this script from awk with one argument and then capture the result in a variable, something like: echo 40 | awk ' { x = $1; "test.sh " x | getline y; print y }... (1 Reply)
Discussion started by: fbg
1 Replies

8. Programming

'getline' questions

I have a C program I've written that includes a 'getline' call that is used to read a line at a time from a file and process the information in each line. A potential problem comes up since I don't know the maximum length of each line; right now I'm using a constant of 256, but that might not be... (2 Replies)
Discussion started by: cleopard
2 Replies

9. UNIX for Dummies Questions & Answers

utility of getline here?

hi , I got a script that I don't understand. awk '{ command="echo "$1 command | getline echome close(command) print $0 "\t" echome }' < user.list It reads the file user.list user.list London John Bridge Peter and sends the stream to the awk command. I don't understand what is... (3 Replies)
Discussion started by: remid1985
3 Replies

10. Shell Programming and Scripting

getline with a unique

I have an inputfile that I trying to awk with a getline(Solaris v8 ksh). However I need to have the results be a unique. Is there a comparable command within awk to return a unique result? Or can you break out of the awk to add a uniq. I am looking for something more complex then a pipe... (6 Replies)
Discussion started by: gozer13
6 Replies
Login or Register to Ask a Question