简体   繁体   中英

how to output paragraphs with spaces from a file using unix/linux grep/awk/sed

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.

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

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.

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

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

ie 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

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 falling if needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM