LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-28-2013, 12:21 AM   #1
Solox
LQ Newbie
 
Registered: May 2013
Posts: 2

Rep: Reputation: Disabled
Question AWK script error - driving me crazy


Hi all,

I am not involved in scripting as the scripts we use have already been written. However, we (my team of 4) are in the midst of migrating from AIX Server platform to a LINUX server platform in which we are responsible for migrating "our" portion.

Through our curent testing, we have discovered several AIX Unix AWK scripts that have compatibility issues with Linux. Most of them we have researched and fixed them. BUT...this one is driving me crazy.

Below is the script:

Code:
BEGIN {
	good_count = 0
	bad_count = 0
	line_count = 0
}

NR == 1 {
	rec_count = substr($0, 15, 8)
        date_str = substr($0, 32, 10) 
		
	print "Date in Header Record : " date_str
}

NR > 1 {
    line_count++
  
    if (length($0) == 710)
    {
        print $0 > GoodFile
        good_count++
    }
    else
    {
        print $0 > BadFile
        bad_count++
    }
}

END {
	if ( line_count == rec_count && bad_count==0 )
	then
    {
   	 print "********************************************"
         print "Count in header        :  " rec_count  
	 print "Count in file received :  " line_count 
	 print "  "
	 print "Number of Good Records :  " good_count
	 print "Number of Bad  Records :  " bad_count  	
    }
    else
    {
	  print "********************************************"
          print "                             PROBLEM WITH FILE SENT FROM SOURCE SYSTEM " 
          print "  "
          print "Count in header        :  "  rec_count  
	  print "Count in file received :  "  line_count 
	  print "  "
          print "Number of Good Records :  " good_count
	  print "Number of Bad  Records :  " bad_count

	  exit 99
    }
}
The error I get when I run it:

Code:
dnt_UIS_header_process.awk: line 1: BEGIN: command not found
dnt_UIS_header_process.awk: line 8: syntax error near unexpected token `('
dnt_UIS_header_process.awk: line 8: `   rec_count = substr ($0, 15, 8)'
I'm at a loss. If somebody can help me uncover WHY is spitting out these errors in Linux while the scripts works in AIX...I would be eternally grateful.

Thank you.
 
Old 05-28-2013, 01:19 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
probably there is a "strange" - non-printable char at the beginning of the script?
you can try od -xc to check it.
 
Old 05-28-2013, 01:25 AM   #3
Ygrex
Member
 
Registered: Nov 2004
Location: Russia (St.Petersburg)
Distribution: Debian
Posts: 666

Rep: Reputation: 68
how do you run this? at first, «then» is not a part of «if» statement in awk, but it seems like the file is executed by shell, not awk
 
2 members found this post helpful.
Old 05-28-2013, 03:51 AM   #4
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
I think you simply missed the header.
Code:
#!/usr/bin/env gawk -f
 
1 members found this post helpful.
Old 05-28-2013, 10:28 AM   #5
murugesan
Member
 
Registered: May 2003
Location: Bangalore ,Karnataka, India, Asia, Earth, Solar system, milky way galaxy, black hole
Distribution: murugesan openssl
Posts: 181

Rep: Reputation: 29
As informed by Ygrex:
Code:
Replace:
           if ( line_count == rec_count && bad_count==0 )
	   then
	   {
With:
 	   if ( line_count == rec_count && bad_count==0 )
	   {

As informed by konsolebox:
Code:
Replace:
           BEGIN {
With:
           /bin/awk 'BEGIN {

Also at the end
Code:
Replace:
	     exit 99
       }
   }
With:
	     exit 99
       }
   }' Any.File.Name.text
 
Old 05-28-2013, 11:20 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Quote:
Originally Posted by murugesan View Post
As informed by konsolebox:
Code:
Replace:
    /bin/awk 'BEGIN {
Code:
    }' Any.File.Name.text
This is incorrect. Your "corrected" version is the shell syntax for launching an awk command. For a standalone script you use #!/bin/awk -f at the top of the script, as konsolebox said, and then go straight into the code without extra quoting.

The same goes for the ending. You don't put the input file directly in the script itself (that's shell syntax again), but use it as an argument when executing the script, or feed it through stdin.

Last edited by David the H.; 05-28-2013 at 11:24 AM. Reason: added second part
 
Old 05-28-2013, 11:43 AM   #7
murugesan
Member
 
Registered: May 2003
Location: Bangalore ,Karnataka, India, Asia, Earth, Solar system, milky way galaxy, black hole
Distribution: murugesan openssl
Posts: 181

Rep: Reputation: 29
Hi,

Kindly look at the posted comments, Replace With for "/bin/awk 'BEGIN {"
>You don't put the input file directly in the script itself
>You don't put the input file directly in the script itself
I feel not to agree with this step.
It depends upon the test also with the type of the document being submitted(Example SAP related document with multi-language document)
based on which the script is being executed.

Yes, agreed with
Code:
#!/bin/awk -f
but based on the writer of the tester to handle the exceptions

I always used to write scripts like on HP-UX OpenSource
Code:
#!/bin/ksh
 
Old 05-28-2013, 12:24 PM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Whether you agree or not, it's still wrong. A stand-alone awk script contains only the code itself, and the shebang line with the interpreter at the top. The input comes from the outside (as an argument or stdin), just as with any other script or command.

You could also put just the code itself in the file, with no shebang, and run it in a shell with "awk -f codefile inputfile". It works the same way.

What you posted is the syntax for a shell wrapper script with an awk command inside it. That will work too, with the proper shell shebang, but it's not an awk script, and it adds an unnecessary second execution layer.

What will not work is using an awk shebang, and then also putting the awk command name in the file after it. That would be telling awk to try to launch itself. Not to mention other syntax errors occurring from improper quoting.

Edit: Here's the (rather brief) gnu awk documentation page on creating stand-alone scripts:

http://www.gnu.org/software/gawk/man...e-Scripts.html

Last edited by David the H.; 05-28-2013 at 12:30 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
RSync error syncing over ssh driving me crazy hazey11 Linux - Server 4 02-26-2012 02:17 PM
Driving me crazy!!! fusion1275 Programming 3 10-15-2008 05:19 PM
I'm trying to install SUSE Linux, but this error is driving me crazy! Hrothgar15 Linux - Hardware 4 08-13-2005 01:10 AM
Help this is driving me crazy!!!!!! Wolfy Linux - Hardware 1 07-07-2004 01:32 AM
Simple shell script is driving me crazy ner Linux - Newbie 4 01-28-2004 10:50 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration