【问题标题】:How to merge lines between two patterns of square brackets如何合并两个方括号模式之间的线
【发布时间】:2019-10-04 13:51:06
【问题描述】:

我想在匹配的方括号行之间合并行,并添加逗号,如下所示

[1] 13:58:13 [FAILURE], webhost1 ,Exited with error code 255
[2] 13:58:14 [SUCCESS], webhost2
cmd1:
Friday October 4 2019 3:12:07 PM
cmd2:
vmw6222......
.........................
[3] 13:58:14 [SUCCESS], webhost3
cmd1:
Friday October 4 2019 3:12:07 PM
cmd2:
vmw6222.........
...........
[30] 15:12:08 [SUCCESS] vmw6211
cmd1:
Friday October 4 2019 3:12:08 PM
[31] 13:58:14 [SUCCESS], webhost4
Stderr: hostn : The term 'hostn' is not recognized as the name of a cmdlet, function, ^M
............................

Expected output
[1] 13:58:13 [FAILURE], webhost1 ,Exited with error code 255
[2] 13:58:14 [SUCCESS], webhost2, cmd1: Friday October 4 2019 3:12:07 PM, cmd2: vmw6222.....
[3] 13:58:14 [SUCCESS], webhost3, cmd1: Friday October 4 2019 3:12:07 PM, cmd2: vmw6222.........
[30] 15:12:08 [SUCCESS] vmw6211, cmd1: Friday October 4 2019 3:12:08 PM
[31] 13:58:14 [SUCCESS], webhost4, Stderr: hostn : The term 'hostn' is not recognized as the name of a cmdlet, function, ^M

我试过Merge lines between two patterns using sed 在下面尝试过,但没有帮助我 任何人都可以帮助我在下面的 awk 命令中出错的地方

awk 'BEGIN {accum_line = "";} /^\[[0-9]+]/{if(length(accum_line)){print accum_line; accum_line = "";}} {accum_line = accum_line " ," $0;} END {if(length(accum_line)){print accum_line; }}'

【问题讨论】:

  • 从您的示例输入/输出中删除任何不是真正数据一部分的...s,因为它们只会使帖子变得混乱,使其不那么清晰。

标签: bash awk sed


【解决方案1】:

请您尝试关注一下。

awk '{printf("%s%s",$0~/^\[/ && FNR>1?ORS:FNR==1?"":OFS,$0)} END{print ""}'  Input_file


解释:

awk '                                                      ##Starting awk program here.
{
  printf("%s%s",$0~/^\[/ && FNR>1?ORS:FNR==1?"":OFS,$0)    ##Using printf to print lines with conditions, explained later in thread.
}
END{                                                       ##Mentioning END section of this awk program here.
  print ""                                                 ##Printing NULL value to print a new line here.
}
'  Input_file                                              ##Mentioning Input_file name here.

(%s%s",$0~/^\[/ && FNR>1?ORS:FNR==1?"":OFS,$0)的解释:

%s%s --> means asking printf to print 2 strings.
on printing 1st string checking condition:
$0~/^\[/ && FNR>1?ORS:FNR==1?"" which means check if a line is starting from [ and NOT 1st line then print ORS(new line) or if a line is 1st line print NULL else print space in each line.
For 2nd string mentioned in printf simply mentioning $0 to print current line.

【讨论】:

【解决方案2】:

假设您输入的...s 应该出现在您的输出中:

$ awk '{printf "%s%s", (/^\[[0-9]+]/ ? ors : ","), $0; ors=ORS} END{print ""}' file
[1] 13:58:13 [FAILURE], webhost1 ,Exited with error code 255
[2] 13:58:14 [SUCCESS], webhost2,cmd1:,Friday October 4 2019 3:12:07 PM,cmd2:,vmw6222......,.........................
[3] 13:58:14 [SUCCESS], webhost3,cmd1:,Friday October 4 2019 3:12:07 PM,cmd2:,vmw6222.........,...........
[30] 15:12:08 [SUCCESS] vmw6211,cmd1:,Friday October 4 2019 3:12:08 PM
[31] 13:58:14 [SUCCESS], webhost4,Stderr: hostn : The term 'hostn' is not recognized as the name of a cmdlet, function, ^M,............................

【讨论】:

  • 谢谢帮助我,但是在 cmd1:, & cmd2:, 之后打印额外的逗号,
  • 如果不是每个换行符,脚本应该如何知道在哪里添加逗号?
  • 只是在cmd1和cmd2前加逗号并合并
  • 我知道你想要什么,但是你编写软件来做一些你需要知道为什么要做的事情。它是基于文字字符串 cmd1cmd2 还是基于位于行块中某个位置的字符串或基于单字行或以 : 结尾的行或其他?
  • 另见my earlier comment,因为在您的示例中使用...s 会使您的预期输出与实际输出不匹配,因此我们不能简单地将预期输出与实际输出进行比较以查看脚本是否工作与否。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-07-10
  • 1970-01-01
  • 2021-10-15
  • 1970-01-01
  • 1970-01-01
  • 2017-02-03
  • 2013-08-15
相关资源
最近更新 更多