1

Working from this answer, I'm using c)

Print the Nth record after some regexp
awk 'c&&!--c;/regexp/{c=N}' file

to summarise test results (i.e. the test fail detail is two lines after the test suite name that starts with '●'), but I noticed there's some occasions I need to instead print out the 4th line below, if the second line matches a string.

Here are the two cases

 ● Test Suite > Use case >  
 
 Type Error: Illegal constructor 

and

● Test Suite > Use case >

Expected test not to call console.error // <--- when I see this...

If the error is expected ...

Warning: Failed prop type ... // <-- ...I want this line

Is there a way to conditionally get the 4th line after if the second line after ● matches a pattern or the second line itself otherwise?

AncientSwordRage
  • 7,086
  • 19
  • 90
  • 173

1 Answers1

0

You probably want to structure it more like a state machine and do different things in different states. First you'll be looking for a test case, and then once you've found it, you'll be scanning for the line you need to print:

BEGIN {
  state="LOOKING";
}

state == "LOOKING" && /^●/ {
  state = "FOUND";
  c = 0;
  next;
}

state == "FOUND" && /^Expected test not to call console\.error/ {
  c = 1;
  next;
}

state == "FOUND" && c == 0 {
  print;
  state = "LOOKING";
  next;
}

state == "FOUND" {
  c--;
}

Note that I based the values to set c to on your description of the input data. But it looks like the data you give in the question has extra blank lines, in which case the lines I think you want to print out are the 3rd and 7th. In that case, change the awk program to set c to 1 when switching the state to FOUND and to 3 when skipping a few more lines after matching /^Expected.../.

onlynone
  • 7,602
  • 3
  • 31
  • 50