Reputation: 905
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
Upvotes: 5
Views: 1810
Reputation: 143047
Add the '{' right after the
BEGIN` 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."
Upvotes: 5