5

This is such a basic question in awk . But I am facing issues in this and I dont know why. problem is when I run the awk command in a single line such as

awk 'BEGIN {} {print $0;}' FILE 

Then the code is running perfecctly

But if I split the code between lines such as

 awk '
 BEGIN
 {
 }
 {
      print $0;
 }' FILE

It gives me an error stating that BEGIN should have an action part . I was wondering since it is the same code that I am formatting, why am I getting this error. Its really important for me to solve this as I would be writting large lines of codes in awk it would be difficult for me to format and bring it in a single line everytime. Could you ppl please help me regarding this. Thank you. Note. I am running this awk in shell environment

NandaKumar
  • 905
  • 4
  • 15
  • 19
  • If you don't need any code in your BEGIN block, you should delete it. Else ... believe what the error messages are telling you! ;-) Try `awk 'BEGIN {}; {print $0;}' FILE` to see if that genterates the error message. Good luck. – shellter Jul 17 '12 at 20:24

1 Answers1

5

Add the '{' right after theBEGIN` and you will not get the error message.

The opening paren { for BEGIN needs to be on the same line as BEGIN. So change what you have

awk '
 BEGIN
 {

to

awk '
 BEGIN {

and you won't get the error message.

The manual does state that "BEGIN and END rules must have actions;", so that may be another problem. This

awk 'BEGIN {} ...

seems a bit odd to me (and there's really no reason to have this if nothing is happening)

@Birei's helpful comment below explains that the way these statements will "parse will be different in both cases. The open '{' in next line is parsed as an action without pattern (not related with BEGIN), while in same line means an empty action of the BEGIN rule."

Levon
  • 138,105
  • 33
  • 200
  • 191
  • 1
    +1, parse will be different in both cases. The open paren in next line is parsed as an action without pattern (not related with `BEGIN`), while in same line means an empty action of the `BEGIN` rule. – Birei Jul 17 '12 at 20:45
  • Of course you can complete your own answer as you wish, don't worry about the credit, I don't mind :-) – Birei Jul 17 '12 at 21:03
  • @Birei: It's not an open paren, it's an open curly brace. – Dennis Williamson Jul 17 '12 at 21:28
  • @DennisWilliamson I'll make the change, but as someone to whom English isn't their first language, I always thought ([{ are all considered parens, and using square, curly, etc were just qualifiers .. the stuff you can learn on SO .. :) – Levon Jul 17 '12 at 21:31
  • In American English, `(` is a parenthesis. In British English, IIRC, it's a "round bracket" – Dennis Williamson Jul 17 '12 at 21:43
  • Thanks a lot for your suggestion guys .. So do I have to have the same for the END too .. i.e END { for END /n { – NandaKumar Jul 17 '12 at 21:53
  • And also how to represent action part – NandaKumar Jul 17 '12 at 21:56
  • @NandaKumar I think that's a fair assumption to make with regard to `END`. – Levon Jul 17 '12 at 21:58