If your question is "can we use nextfile inside ENDFILE section" Then short answer is no, we can't use it.
Here is an example:
- Use of
nextfile in BEGINFILE section: Let's say we have 2 Input_files named file1 and file2.
Let us run nextfile inside BEGINFILE rule.
awk 'BEGINFILE{print FILENAME;nextfile} 1' file1 file2
file1
file2
We could see its printing Input_file names in it and NOT printing the contents of files, means its working fine.
- Use of
nextfile inside ENDFILE block: Now lets run nextfile inside ENDFILE section and see what happens.
awk '1;ENDFILE{nextfile;print "bla bla"}' file1 file2
awk: cmd. line:1: error: `nextfile' used in ENDFILE action
It gives an error as you see above. Why this is happening, because ENDFILE OR END blocks always get executed after all the records of Input_file(s) are done with executing, so in case you want to jump to next Input_file then you could either use it in BEGINFILE OR BEGIN section OR use it in main block of your code in case you want to match specific condition and then jump to next Input_file.
How to skip ENDFILE section for a specific Input_file: If you have an ENDFILE statement then on each Input_file's record reading, then to skip a specific Input_file we could use condition like this:
awk '1;ENDFILE{if(FILENAME!="file2"){print FILENAME}}' file1 file2
Output will be as follows.
bla bla bla file1 contents here.....
file1
bla bnla bla file2 contents here....
Basically its print contents of Input_file1 and Input_file2 but because a condition is mentioned in ENDFILE block its NOT printing file2 name in output, hence prevented a statements from execution in ENDFILE block here.
Ideal use of nextfile in BEGINFILE block: If we use nextfile inside BEGINFILE block then it will for sure NOT going to print any of the records of that specific Input_file, why because BEGINFILE block's statements gets executed before reading records from Input_file when we mention nextfile it will NOT start reading records process and will simply jump to next Input_file.
So when to use nextfile in BEGINFILE block?: IMHO one of the BEST use case will be when we want to process multiple Input_file(s) and want to deal with headers for each Input_file(eg--> print only header OR a message for specific file and DO NOT process records of that Input_file). OR 2nd case I could think of if any processing you need to do before executing the records of that Input_file.