簡體   English   中英

如何使用 unix/linux grep/awk/sed 從文件中使用空格 output 段落

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

我有一個包含以下日志的文件

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`

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

我基本上只想 output 低於1 failing ,我希望它停在虛線處。 如何使用 grep、awk 或 sed 執行此操作?

我試過awk '/failing/' RS="\n\n" ORS="\n\n" sample2awk -v RS='' -v '/failing/' sample2但所有輸出都是1 failing和沒有其他的。

使用sed ,您可以這樣做:

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

或者,如果您想跳過標記行,請使用:

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

使用awk您可以使用:

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

使用非常簡單:

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

將打印:

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`

邏輯是,當找到我們感興趣的字符串時, 1 failing ,我們將標志設置為 true ( found )並存儲當前行( s )。 然后 awk 將遍歷文件中的所有行。 我們可以檢查兩個條件。 找到的標志為真,並且當前行與s不同。 當條件滿足時,打印該行。

我認為你真正想做的是:

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

即在剛剛failing的行之后打印行,前面是任意數字和可選的周圍空格。 這是基於您的文本(強調在我的below )和代碼(僅測試failing )在您的問題結束時:

我基本上只想 output低於1 的所有內容都失敗...我已經嘗試過awk '/failing/' RS="\n\n" ORS="\n\n" sample2awk -v RS='' -v '/failing/' sample2

好吧,由於被標記,使用 grep 和 Perl 兼容的正則表達式(如果可用,至少 GNU grep):

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

如果需要,考慮擴大1 falling的正則表達式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM