ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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
Location: Bangalore ,Karnataka, India, Asia, Earth, Solar system, milky way galaxy, black hole
Distribution: murugesan openssl
Posts: 181
Rep:
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
Whether you agree or not, it's still wrong. A stand-aloneawk 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:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.