如何捕获XTH模式1到模式2

发布于 2025-01-24 13:37:48 字数 684 浏览 2 评论 0原文

这是我解释我的问题的示例:

Bug Day 2022-01-13:
Security-Fail 248975
Resolve:
...
Bug Day 2022-01-25:
Security-Fail 225489
Security-Fail 225256
Security-Fail 225236
Resolve:
...
Bug Day 2022-02-02:
Security-Fail 222599
Resolve:

因此,我有一个包含多个安全漏洞的大文件。 我想获得这一点:

2022-01-13;248975
2022-01-25;225489,225256,225236
2022-02-02;222599

我要做类似

bugDayNb=$(grep "Bug Day" | wc -l)

for i in $bugDayNb; do
    echo "myBugsFile" | grep -A10 -m$i "Bug Day"
done

此命令的问题是,如果有10个以上的安全性失效,它将行不通,如果我放置“ -a50”,它可能会采取下一个安全性 - 下一个错误日。

因此,我希望一种方法 sed 或类似的东西,从xth“ bug day”到xth“ resolve”

谢谢!

this is my example to explain my question :

Bug Day 2022-01-13:
Security-Fail 248975
Resolve:
...
Bug Day 2022-01-25:
Security-Fail 225489
Security-Fail 225256
Security-Fail 225236
Resolve:
...
Bug Day 2022-02-02:
Security-Fail 222599
Resolve:

So, I have a big file that contain multiple security vulnerabilities.
I want to obtain that :

2022-01-13;248975
2022-01-25;225489,225256,225236
2022-02-02;222599

I though about doing something like

bugDayNb=$(grep "Bug Day" | wc -l)

for i in $bugDayNb; do
    echo "myBugsFile" | grep -A10 -m$i "Bug Day"
done

The problem of this command is, if there are more than 10 Security-Fail, it won't works, and if I put a "-A50" it may take the next Security-Fail of the next Bug Day.

So I would prefer a way to sed or something like that from xth "Bug Day" to xth "Resolve"

Thank you !!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

眸中客 2025-01-31 13:37:48

这是一种方法:

$ awk '/^Bug Day/{d=$NF; s=""}
       /^Security-Fail/{d = d s $NF; s=","}
       /^Resolve:/{print d}' ip.txt
2022-01-13:248975
2022-01-25:225489,225256,225236
2022-02-02:222599
  • /^bug Day/{d = $ nf; s =“”}将日期保存到变量d如果行以bug Day 开始,然后初始化s到空字符串
    • 使用{d = $ nf; sub(/:$/,“;”,d); s =“”}如果要;而不是:
  • /^security-fail/{d = d = ds $ nf; s =“,”}当行以Security-Fail将数字附加到d变量并设置S时,以便进一步附录将通过
  • /^分离:/{print D}resolve:时打印结果。

Here's one way to do it:

$ awk '/^Bug Day/{d=$NF; s=""}
       /^Security-Fail/{d = d s $NF; s=","}
       /^Resolve:/{print d}' ip.txt
2022-01-13:248975
2022-01-25:225489,225256,225236
2022-02-02:222599
  • /^Bug Day/{d=$NF; s=""} save the date to variable d if line starts with Bug Day and initialize s to empty string
    • use {d=$NF; sub(/:$/, ";", d); s=""} if you want ; instead of :
  • /^Security-Fail/{d = d s $NF; s=","} when line starts with Security-Fail append the number to d variable and set s so that further appends will be separated by ,
  • /^Resolve:/{print d} print the results when Resolve: is seen
離人涙 2025-01-31 13:37:48

使用您显示的样本,请尝试以下内容AWK程序。

awk '
/Bug Day/{
  sub(/:$/,"",$NF)
  bugVal=$NF
  next
}
/^Security-Fail/{
  secVal=(secVal?secVal ",":"")$NF
  next
}
/^Resolve:/ && bugVal && secVal{
  print bugVal";"secVal
  bugVal=secVal=""
}
'  Input_file

说明: 添加了上述详细说明。

awk '                               ##Starting awk program from here.
/Bug Day/{                          ##Checking condition if line contains Bug day then do following.
  sub(/:$/,"",$NF)                  ##Substituting : at last of $NF in current line.
  bugVal=$NF                        ##Creating bugVal which has $NF value in it.
  next                              ##next will skip all further statements from here.
}
/^Security-Fail/{                   ##Checking if line starts from Security-Fail then do following.
  secVal=(secVal?secVal ",":"")$NF  ##Creating secVal which has $NF value in it and keep adding value to it with delimiter of comma here.
  next                              ##next will skip all further statements from here.
}
/^Resolve:/ && bugVal && secVal{    ##Checking condition if line starts from Resolve: and bugVal is SET and secVal is SET then do following.
  print bugVal";"secVal             ##printing bugVal semi-colon secVal here.
  bugVal=secVal=""                  ##Nullifying bugVal and secVal here.
}
'  Input_file                       ##mentioning Input_file name here. 

With your shown samples, please try following awk program.

awk '
/Bug Day/{
  sub(/:$/,"",$NF)
  bugVal=$NF
  next
}
/^Security-Fail/{
  secVal=(secVal?secVal ",":"")$NF
  next
}
/^Resolve:/ && bugVal && secVal{
  print bugVal";"secVal
  bugVal=secVal=""
}
'  Input_file

Explanation: Adding detailed explanation for above.

awk '                               ##Starting awk program from here.
/Bug Day/{                          ##Checking condition if line contains Bug day then do following.
  sub(/:$/,"",$NF)                  ##Substituting : at last of $NF in current line.
  bugVal=$NF                        ##Creating bugVal which has $NF value in it.
  next                              ##next will skip all further statements from here.
}
/^Security-Fail/{                   ##Checking if line starts from Security-Fail then do following.
  secVal=(secVal?secVal ",":"")$NF  ##Creating secVal which has $NF value in it and keep adding value to it with delimiter of comma here.
  next                              ##next will skip all further statements from here.
}
/^Resolve:/ && bugVal && secVal{    ##Checking condition if line starts from Resolve: and bugVal is SET and secVal is SET then do following.
  print bugVal";"secVal             ##printing bugVal semi-colon secVal here.
  bugVal=secVal=""                  ##Nullifying bugVal and secVal here.
}
'  Input_file                       ##mentioning Input_file name here. 
家住魔仙堡 2025-01-31 13:37:48

这可能对您有用(gnu sed):

sed -nE '/Bug Day/{:a;N;/Resolve/!ba;s/.* //mg;y/\n/,/;s/:,(.*),.*/;\1/p}' file

bug Dayresolve和格式相应地收集行。

如果您想对一天或几天的选择有选择性,请使用:

sed -nE '/Bug Day/{x;s/^/x/;/^x{1,3}$/!{x;d};x
         :a;N;/Resolve/!ba;s/.* //mg;y/\n/,/;s/:,/;/;s/(.*),.*/\1/p}' file

以上命令显示前3天IE 1至3

This might work for you (GNU sed):

sed -nE '/Bug Day/{:a;N;/Resolve/!ba;s/.* //mg;y/\n/,/;s/:,(.*),.*/;\1/p}' file

Gather up lines between Bug Day and Resolve and format accordingly.

If you want to be selective about a single day or range of days, use:

sed -nE '/Bug Day/{x;s/^/x/;/^x{1,3}$/!{x;d};x
         :a;N;/Resolve/!ba;s/.* //mg;y/\n/,/;s/:,/;/;s/(.*),.*/\1/p}' file

The above command displays the first 3 days i.e. 1 to 3

风吹短裙飘 2025-01-31 13:37:48

您能尝试尝试awk解决方案:

awk '/^Bug Day/ {f=1; line=$0; next}                    # start of block
    f {line=line ORS $0}                                # append the line if "f" is set
    /^Security-Fail/ {g=1}                              # the block contains "Security-Fail"
    /^Resolve/ {if (g) print line; f=g=0; line=""}      # end of block
' input_file

如果您喜欢单线:

awk '/^Bug Day/{f=1; line=$0; next} f{line=line ORS $0} /^Security-Fail/{g=1} /^Resolve/{if (g) print line; f=g=0; line=""}' input_file

Would you please try an awk solution:

awk '/^Bug Day/ {f=1; line=$0; next}                    # start of block
    f {line=line ORS $0}                                # append the line if "f" is set
    /^Security-Fail/ {g=1}                              # the block contains "Security-Fail"
    /^Resolve/ {if (g) print line; f=g=0; line=""}      # end of block
' input_file

If you prefer a one-liner:

awk '/^Bug Day/{f=1; line=$0; next} f{line=line ORS $0} /^Security-Fail/{g=1} /^Resolve/{if (g) print line; f=g=0; line=""}' input_file
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文