onlyf
onlyf

Reputation: 883

awk - cannot open file error

#!/usr/bin/nawk -f

# Before lines are processed
BEGIN {

print "Awk Started" >> FILE_STDOUT

# Variables
rec1=""
rec2=""
rec3=""
rec4=""
mod=0
X=23

}

 NR % 5 != 0 {
 mod=NR%5
if (mod==1) 
    rec1=$0
else
if (mod=2)    
    rec2=$0
else
if (mod=3)   
    rec3=$0
else 
if (mod=4)  
  rec4=$0

next
}

# NR % 5 == 0
{

  if (substr($rec1,9,2)==X || substr($rec2,9,2)==X || substr($rec3,9,2)==X || substr($rec4,9,2)==X)
{
   print rec1
   print rec2
   print rec3
   print rec4
}
}

END {

   print "Awk Ended" >> FILE_STDOUT

}

While running the above script which is supposed to check blocks of 4 for a specific entry and output them, i get this error :

awk: cannot open "" for output (No such file or directory)

What's the reason?

Sample input :

    101010201231000
    101010201AA1000
    101010201AA1000
    101010201AA1000
    303030401AA1000
    303030401321000
    303030401AA1000
    303030401AA1000
    505050601AA1000
    505050601AA1000
    505050601431000
    505050601AA1000
    707070801AA1000
    707070801AA1000
    707070801AA1000
    707070801561000

Sample output :

101010201231000
101010201AA1000
101010201AA1000
101010201AA1000

The error i am given is :

nawk: program limit exceeded: maximum number of fields size=32767 FILENAME="awktestor.txt" FNR=1 NR=1

Upvotes: 0

Views: 2634

Answers (1)

fedorqui
fedorqui

Reputation: 290025

When you say:

print "Awk Started" >> FILE_STDOUT

you are redirecting the print output to a file, whose value is stored in the variable FILE_STDOUT. However, if this variable is not set, awk cannot redirect and it fails.

It is as if you would say:

awk 'BEGIN {print 1>>FILE_STDOUT}'

Which returns

awk: cmd. line:1: fatal: expression for `>>' redirection has null string value


Also, it seems to be that you want to match a line when 23 is found in the 10th and 11th character. Then, print that line together with the following 3 lines.

For that you can say:

awk '$1 ~ /^.{9}23/ {c=4} c&&c--' file
    101010201231000
    101010201AA1000
    101010201AA1000
    101010201AA1000

Upvotes: 1

Related Questions