Как объединить строки между двумя узорами квадратных скобок - PullRequest
0 голосов
/ 04 октября 2019

Я хочу объединить строки между соответствующими квадратными скобками и добавить запятую, как показано ниже

[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

Я пробовал Объединить строки между двумя шаблонами, используя 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; }}'

Ответы [ 2 ]

3 голосов
/ 04 октября 2019

Не могли бы вы попробовать следующее.

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.
0 голосов
/ 04 октября 2019

Предполагая, что ... 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,............................
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...