[英]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" sample2
和awk -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 :使用awk非常简单:
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
andawk -v RS='' -v '/failing/' sample2
我基本上只想 output低于1 的所有内容都失败...我已经尝试过
awk '/failing/' RS="\n\n" ORS="\n\n" sample2
和awk -v RS='' -v '/failing/' sample2
Well, as the grep and regex are tagged, using grep and Perl-compatible regular expressions (where available, GNU grep at least):好吧,由于grep和正则表达式被标记,使用 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.