简体   繁体   English

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

[英]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.我基本上只想 output 低于1 failing ,我希望它停在虚线处。 how can I do this using grep,awk or sed?如何使用 grep、awk 或 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.我试过awk '/failing/' RS="\n\n" ORS="\n\n" sample2awk -v RS='' -v '/failing/' sample2但所有输出都是1 failing和没有其他的。

Using sed , you may do this:使用sed ,您可以这样做:

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您可以使用:

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 ).逻辑是,当找到我们感兴趣的字符串时, 1 failing ,我们将标志设置为 true ( found )并存储当前行( s )。 awk will then iterate over all the lines in the file.然后 awk 将遍历文件中的所有行。 We can check for two conditions.我们可以检查两个条件。 The flag found is true and that the current line is different than s .找到的标志为真,并且当前行与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.即在刚刚failing的行之后打印行,前面是任意数字和可选的周围空格。 That's based on your text (emphasis on below mine) and code (which only tests for failing ) at the end of your question:这是基于您的文本(强调在我的below )和代码(仅测试failing )在您的问题结束时:

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我基本上只想 output低于1 的所有内容都失败...我已经尝试过awk '/failing/' RS="\n\n" ORS="\n\n" sample2awk -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 和 Perl 兼容的正则表达式(如果可用,至少 GNU grep):

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

Consider widening the regex for 1 falling if needed.如果需要,考虑扩大1 falling的正则表达式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从文件中提取特定字符串,然后使用grep,awk,sed输出到另一个文件 - Extract a particular string from a file and output to another file using grep, awk, sed 如何使用 sed、awk、Z4A037BAC753C8548F24Z、awk、Z4A037BAC753C8548F24 将跨多个文件的一行的 substring 中的空格替换为 %20 - How to substitute spaces with %20 in a substring of a line across multiple files using sed, awk, grep etc 如何使用sed,awk或grep等Linux程序从HTML选择列表中删除唯一值? - How to remove unique values from an HTML select list with linux program like sed, awk, or grep? 如何grep / sed / awk以一个以空白字符开头的输出范围 - How to grep/sed/awk for a range of output starting with a whitespace character 如何使用sed / awk / grep ...从文件中提取块,每个块将其保存到文件中 - How to use sed / awk / grep … to extract blocks from a file and each block save it into a file 使用Grep Sed或Awk搜索和替换EDL文件中的行 - Using Grep Sed or Awk to search and replace lines in EDL file 使用SED,AWK或GREP匹配文件中的URL模式 - Match URL pattern within file using SED, AWK or GREP 使用sed / awk / grep格式化git log输出 - Formatting git log output with sed/awk/grep 保存的格式输出在[grep,sed,awk或?]列中 - Saved format output in columns [grep, sed, awk or ?] 多行输出的正则表达式 - sed / awk / grep - Regex for multiline output - sed / awk / grep
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM