-1

I have a file which contains the following logs

Get Authentication token from url
    1) should successfully return a token

  Get profile
    ✓ should successfully return a profile 

  Create a user profile 
    ✓ Should successfully create a new profile 


  Get a 400 Error when using invalid URL
    ✓ should return 400 status code )

  3 passing (9s)
  1 failing

  1) Get Authentication token from url
       should successfully return an access token:
     CypressError: `cy.request()` failed on:

https://testurl.com

The response we received from your web server was:

  > 400: Bad Request

This was considered a failure because the status code was not `2xx` or `3xx`.

If you do not want status codes to cause failures pass the option: `failOnStatusCode: false`

-----------------------------------------------------------

I basically want to just output everything below 1 failing and I want it to stop at the dashed line. how can I do this using grep,awk or sed?

I have tried awk '/failing/' RS="\n\n" ORS="\n\n" sample2 and awk -v RS='' -v '/failing/' sample2 but all that outputs is 1 failing and nothing else.

user6248190
  • 1,233
  • 2
  • 16
  • 31

4 Answers4

2

Using sed, you may do this:

sed -En '/^[[:space:]]*[0-9]+ failing/,$p' file

or if you want to skip marker lines then use:

sed -En '/^[[:space:]]*[0-9]+ failing/,/^---*/{ //!p; }' file

Using awk you can use:

awk '/^-+/{p=0}; p; /^[[:space:]]*[0-9]+ failing/{p=1}' file
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Note that the sed version will work with GNU sed but C escape sequences like `\t` are not standard. The bracket expression `[\t]` matches `\ ` or `t` in POSIX sed. The [awk spec explicitly extends ERE](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/awk.html#tag_20_06_13_04) to support them. – luciole75w Aug 05 '20 at 02:43
  • `[ \t]` worked on my BSD and gnu sed versions. To make it compatible with order POSIX sed versions one can use `sed -n '/^[[:space:]]*1 failing/,$p' file` – anubhava Aug 05 '20 at 08:05
  • 1
    Indeed `[[:space:]]` is standard and works fine. I just tested again a simple `sed 's/[\t]/x/'` on FreeBSD 12.1 and I confirm that it fails to match tab characters (but it matches literal backslashes and `t`'s). – luciole75w Aug 05 '20 at 09:09
  • @anubhava how can I stop the output at the `-----------------------------------------------------------` line ? – user6248190 Aug 07 '20 at 11:25
1

Very simple with :

awk '/\<1 failing/ {s=$0;found=1} {if(found && $0!=s) print}' file

will print:

1) Get Authentication token from url
       should successfully return an access token:
     CypressError: `cy.request()` failed on:

https://testurl.com

The response we received from your web server was:

  > 400: Bad Request

This was considered a failure because the status code was not `2xx` or `3xx`.

If you do not want status codes to cause failures pass the option: `failOnStatusCode: false`

The logic is that when the string we are interested in is found, 1 failing, we set a flag to true (found) and we store the current line (s). awk will then iterate over all the lines in the file. We can check for two conditions. The flag found is true and that the current line is different than s. When the conditions are met, the line is printed.

Paolo
  • 21,270
  • 6
  • 38
  • 69
1

I think what you're really trying to do is:

awk 'f{print; next} /^[[:space:]]*[0-9]+[[:space:]]+failing[[:space:]]*$/{f=1}' file

i.e. print the lines after a line that is just failing preceded by any number and optional surrounding spaces. That's based on your text (emphasis on below mine) and code (which only tests for failing) at the end of your question:

I basically want to just output everything below 1 failing ... I have tried awk '/failing/' RS="\n\n" ORS="\n\n" sample2 and awk -v RS='' -v '/failing/' sample2

Ed Morton
  • 188,023
  • 17
  • 78
  • 185
0

Well, as the and are tagged, using grep and Perl-compatible regular expressions (where available, GNU grep at least):

$ grep -Pzo "(?<=1 failing\n)(.*\n)*" file

Consider widening the regex for 1 fallingif needed.

James Brown
  • 36,089
  • 7
  • 43
  • 59