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.
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'.
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 '.
END pattern: means that Awk will execute the action(s) specified in END before it actually exits.
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With