user8717305
user8717305

Reputation:

How to merge lines between two patterns of square brackets

I want to merge lines between matching square bracket lines,and add comma like below

[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

I tried Merge lines between two patterns using sed Tried below , but didn't helping me can anyone help me where i wrong in below awk command

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; }}'

Upvotes: 0

Views: 348

Answers (2)

RavinderSingh13
RavinderSingh13

Reputation: 133528

Could you please try following.

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


Explanation:

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.

Explanation of (%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.

Upvotes: 2

Ed Morton
Ed Morton

Reputation: 203607

Assuming the ...s from your input should appear in your output:

$ 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,............................

Upvotes: 0

Related Questions