首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何将两种方括号模式之间的行合并

如何将两种方括号模式之间的行合并
EN

Stack Overflow用户
提问于 2019-10-04 13:51:06
回答 2查看 244关注 0票数 0

我希望在匹配的方括号行之间合并行,并在下面添加逗号

代码语言:javascript
运行
复制
[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在下面尝试,但没有帮助我

代码语言:javascript
运行
复制
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; }}'
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-10-04 13:59:39

你能试一下吗。

代码语言:javascript
运行
复制
awk '{printf("%s%s",$0~/^\[/ && FNR>1?ORS:FNR==1?"":OFS,$0)} END{print ""}'  Input_file

解释:

代码语言:javascript
运行
复制
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)解释

代码语言:javascript
运行
复制
%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
EN

Stack Overflow用户

发布于 2019-10-04 14:14:28

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

代码语言:javascript
运行
复制
$ 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,............................
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58237760

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档