1828533 Members
2451 Online
109980 Solutions
New Discussion

Re: awk problem

 
Gemini_2
Regular Advisor

awk problem

when I run

awk '/abc|cde/ END{ print "--->" $1}' file
awk: cmd. line:1: /abc|cde/ END{ print "--->" $1}
awk: cmd. line:1: ^ parse error

I got parse error...why?

hmmm..

thank u
16 REPLIES 16
James R. Ferguson
Acclaimed Contributor

Re: awk problem

Hi:

This works for me as written (and I cut-and-pasted from this post).

Regards!

...JRF...
A. Clay Stephenson
Acclaimed Contributor

Re: awk problem

I don't see anything wrong at all. Is this HP-UX?
If it ain't broke, I can fix that.
Rodney Hills
Honored Contributor

Re: awk problem

It ran ok on my system.

Could you have an unprintable character in the command?

Rod Hills
There be dragons...
Rodney Hills
Honored Contributor

Re: awk problem

Is it possible you have "awk" aliased to something else?

Enter "alias" and look to see if awk is defined.

Rod Hills
There be dragons...
Gemini_2
Regular Advisor

Re: awk problem

I dont think that I have any unprintable characters..

very change.....

I am using linux system, does it matter?

awk is just awk right?
Rodney Hills
Honored Contributor

Re: awk problem

For the most part. awk is awk.

The history of awk includes a version called "new awk" or "nawk", then you have the gnu version.

Although your line is very simple and should be runable. Try replacing the "--->" with just "###". In case the ">" is being processed by awk for file redirection.

Rod Hills
There be dragons...
James R. Ferguson
Acclaimed Contributor

Re: awk problem

Hi (again):

Well, 'awk' is 'awk' just like UNIX is UNIX. There *ARE* variations. You might see if you have a "new" awk --- 'nawk'.

Regards!

...JRF...
A. Clay Stephenson
Acclaimed Contributor

Re: awk problem

No awk is not just awk. The Gnu awk which is almost certainly your Linux version. The nawk (new awk - at least 20 years old which is HP-UX} and there is oawk (old awk) oawk is 'awk' on Solaris; nawk is awk on HP-UX; and gawk is awk on Linux.

I suspect that if you remove the 'END' your script will run.
If it ain't broke, I can fix that.
Gemini_2
Regular Advisor

Re: awk problem

Yes, if I took out "END", it will run, but I need END to do OFS=":"

gawk '/abc|cde/ END { OFS=":", print "**" $1}' file

thank you for trying...
maybe gnu awk works funny..
James R. Ferguson
Acclaimed Contributor

Re: awk problem

Hi (again):

Try adding a semicolon after the regular expression and before the END block.

Regards!

...JRF...
A. Clay Stephenson
Acclaimed Contributor

Re: awk problem

Okay. In that case you need a null statement. Insert a ';' (without the quotes) before the END. That should work on all awk flavors. I suspect a newline would work as well but you have quoted this one-liner so a ; as a null statement is probably the better choice.
If it ain't broke, I can fix that.
Gemini_2
Regular Advisor

Re: awk problem

I did try to use ";" but if I do that, I got a different result.

see below

awk '/abc|cde/; END{ print "--->" $1}' file
abc test
cde test
--->test

awk '/abc|cde/ END{ print "--->" $1}' file
awk: cmd. line:1: /abc|cde/ END{ print "--->" $1}
awk: cmd. line:1: ^ parse error

awk '/abc|cde/ { print "--->" $1}' file
--->abc
--->cde


the last one is the right answer.



James R. Ferguson
Acclaimed Contributor

Re: awk problem

Hi (again):

The last one is the CORRECT result based on what you wrote. The inference is to print lines that match the regular expresssion. Then, the END block is executed to print the "--->" and the first field of the last record processed.

If all you want is to print "--->" in front of matches do:

# awk '/abc|cde/ { print "--->" $1}' file

Of course, this assumes that the first field ($1) is what you want to point at.

Regards!

...JRF...


Gemini_2
Regular Advisor

Re: awk problem

thank you all..

I ended up using BEGIN..and rewrote my awk statement....

I will assign everyone some points for helping out!!

thank you very much AGAIN

A. Clay Stephenson
Acclaimed Contributor

Re: awk problem

It would probably make more sense to describe what you want done rather than telling us about parsing errors.

Even your example is not clear because your input file is so sparse. With only the last line left as input, it's not easy to follow your expectations.

One trick would be to use "{exit}" as soon as a pattern match is found. The exit function imediately triggers the END block (if it exists).
If it ain't broke, I can fix that.
Gemini_2
Regular Advisor

Re: awk problem

yes, you are right.

I realized that I should give a sample file and a sample output, then it will much better.

next time!!

thanks all anyway!!