Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Skip processing current pattern and continue with next

Status
Not open for further replies.

David Klein

Programmer
Feb 6, 2020
2
IL
I want to stop processing the action of the current pattern and continue with the next pattern.
I am familiar with "continue", "break" and "next" but none seem to do what I want.

For example:

pattern1 {
many lines, nested loops etc.​

if (condition) Skip_rest_of_pattern1_and_continue_with_pattern2​

many lines, nested loops etc.​
}

pattern2 {
more code​
}

 
When you are on pattern1 and if condition occurs you simply do nothing and skip to the next pttern2.

For example you have a file:
david_klein.txt
Code:
line 1 bar
line 2
line 3 foo bar baz
line 4
line 5

and this script:
david_klein.awk
Code:
# awk -f david_klein.awk david_klein.txt
/bar/ {
  if ($0 ~ /baz/) {
    # skip
  } 
  else {
    print "found bar: " $0
  } 
}

/foo/ {
  print "found foo: " $0    
}

If you are on a line which contains the pattern /bar/ and if the condition ($0 ~ /baz/) occurs you can simply do nothing and skip to the next pattern which is /foo/
Output:
Code:
$ awk -f david_klein.awk david_klein.txt
found bar: line 1 bar
found foo: line 3 foo bar baz
 
Hi

microm, I think the OP wants to return early from a more complex condition hell.
[pre][small]
pattern1 {
many lines, nested loops etc.

if (condition) {
many lines, nested loops etc.

if (condition) {
many lines, nested loops etc.

if (condition) {
Skip_rest_of_pattern1_and_continue_with_pattern2
}

many lines, nested loops etc.
} else {
many lines, nested loops etc.
}

many lines, nested loops etc.
} else {
many lines, nested loops etc.
}

many lines, nested loops etc.
}

pattern2 {
more code
}
[/small][/pre]
Personally I would think about moving pattern1's action in a dummy function from where to be able to [tt]return[/tt] in any desired moment, then call only that dummy function on pattern1. Ugly, clumsy, efficient.


Feherke.
feherke.github.io
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top