Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

awk exiting the ACTION path and going directly to END part

Tags:

shell

awk

I am using an awk script and the skeleton of the same is simple

 awk '
 BEGIN {
     Variable declaration
 }
 {
    ACTION PART
 }
 END
 {
 }' FILE A

The file A is such a huge file. So I wanted not to traverse the entire file and so what I am trying to do is, I am trying to keep some checks in ACTION PART in such a way that if that check is successful, then I need to skip reading the rest part of the file and directly go to END part.

My question is how would I redirect the script from ACTION PART to END Part based on the condition.. I am looking for some kind of command like "break" in for loop. Could you people share your ideas. Thank you.

like image 300
NandaKumar Avatar asked Jul 18 '12 10:07

NandaKumar


People also ask

How do you terminate an awk?

Using the awk Command. Like sed's 'q [exit-code]', awk has the exit [expression] command to exit the awk script with a return value, such as 'exit 100'.

How do you use awk begin and end?

For example: $ awk ' > BEGIN { print "Analysis of \"li\"" } > /li/ { ++n } > END { print "\"li\" appears in", n, "records." }' mail-list -| Analysis of "li" -| "li" appears in 4 records. This program finds the number of records in the input file mail-list that contain the string ' li '.

What is awk end?

END pattern: means that Awk will execute the action(s) specified in END before it actually exits.

How do I use Getline in awk?

The getline command returns 1 if it finds a record and 0 if it encounters the end of the file. If there is some error in getting a record, such as a file that cannot be opened, then getline returns -1. In this case, gawk sets the variable ERRNO to a string describing the error that occurred.


1 Answers

The exit command will do what you want.

From the man page:

Similarly, all the END blocks are merged, and executed when all the input is exhausted (or when an exit statement is executed).

like image 113
Dennis Williamson Avatar answered Oct 20 '22 15:10

Dennis Williamson