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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Awk questions 1

Status
Not open for further replies.

stu78

Programmer
May 29, 2002
121
0
0
GB
Hi,

I am new to awk, and am attempting some practice scripts; I wish to use the getline function, however no matter how simple the example is, I cannot get it to run. Instead I get something like;

awk: syntax error near line 2
awk: illegal statement near line 2


This is the script I am running; does anybody have any suggestions?

awk '{
getline < &quot;secondary.input&quot;
print
}'

 
What awk you are using is going to be important here I'm afraid.

Try this:
awk ' BEGIN {
if (getline val < &quot;secondary.input&quot;) {
print val
}
}'
 
hi,

I got the same error message when I ran this one... that is

awk: syntax error near line 2
awk: illegal statement near line 2

how do I get what version I am running - once again I am new to awk...
 
Hi stu78,

It looks like version doesn't recognize getline. Let's try to prove or disprove this hypothesis beyond all shadow of a doubt. We can do this by eliminating all extraneous elements. Please try running the following on the command line:

echo &quot;x&quot; > nonfile.txt ; awk '{getline}' nonfile.txt

BTW, the reference to 'nonfile.txt' is just to provide a bogus file. I am trusting that no file of that name would already exist on your box.

If your awk recognizes the 'getline' command, it should just come back to the command line with no output. If your version doesn't recognize getline you should get an error message similar to the one you got before. Probably something like:

UX:awk: ERROR: Illegal statement
UX:awk: INFO: Input record number 1, file nofile.txt
UX:awk: INFO: Source line number 1

If you do get the above message, or a similar one, I would recommend that you just abandon your current version and make arrangements to install a more current version.

Good luck,
Grant.
 
Thanks Grant,

The line
echo &quot;x&quot; > nonfile.txt ; awk '{getline}' nonfile.txt
did not return any errors;

Do you have any ideas on how I can use the getline function on this?

stu
 
Hi stu78,

Well, it seems we have proved that getline itself is acceptable to your version of awk.

Going back to your original example, perhaps it did not like the redirection syntax, ie:
getline < &quot;secondary.input&quot;

Please try replacing that line with:
&quot;cat secondary.input&quot; | getline

By the way, I am assuming that 'secondary.input' really is a text file. Is there any doubt about this?

Grant.
 
Your awk probably needs the full pathname of the secondary.input file. Try it with the full pathname.
 
Hi Grant,

This is what I typed on the command line;

awk ' BEGIN { if (&quot;cat secondary.input&quot; | getline) { print } }'

& this is the output....

awk: syntax error near line 1
awk: illegal statement near line 1
awk: bailing out near line 1

I havn't got this to work as you can see... If you have any advice it would be appreciated.... Also, the secondary.input file does exist.....
 
Hmmm,

I am puzzled.

I ran the identical code on my box and, as expected, it works just fine. Therefore it's not syntax.

My conclusion is that getline exists on your system, but it seems to be too primitive to support the functionality that we take for granted. Apparently, you just have an old version of awk.

Possibly we could find out just what your getline is capable of and try to find work-arounds for it, but that would probably just be a needless expense of effort.

I suggest you try to arrange for a newer version of awk to be installed. It's free.

Good luck,
Grant.
 
I'll give it a go... Thanks for all your help Grant.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top