How to merge fields in a single file using awk ?

Hi,

From a file, using:

awk -F" " '{ if (NF == 6) print $1, $2, $3, $4, $5, $6; if (NF == 5) print $1, $2, $3, $4, $5; }' 

i printed out the required output. But i'm trying to merge the columns. Please look at the desired output. Any suggestions? Thanks

Output:

00015 PSA1                13     2     1  P
         PSA2                12     1     1  C
00016 PSA1                13     2     1  P
         PSA2                12     1     1  C
00017 PSA1                13     2     1  P
         PSA2                12     1     1  C
00018 PSA1                13     2     1  P
         PSA2                12     1     1  C
00019 PSA1                13     2     1  P
         PSA2                12     1     1  C
00020 sg1                 240     0     1  .
00021 sg1                 240     0     1  .
00022 sg1                 240     0     1  .
00023 sg1                 240     0     1  .
00024 sg1                 240     0     1  .

desired output:

00015 PSA1,PSA2                13,12     2,1     1,1  P,C
00016 PSA1,PSA2                13,12     2,1     1,1  P,C                     
00017 PSA1,PSA2                13,12     2,1     1,1  P,C
00018 PSA1,PSA2                13,12     2,1     1,1  P,C
00019 PSA1,PSA2                13,12     2,1     1,1  P,C
00020           sg1                   240        0       1     .
00021           sg1                   240        0       1     .
00022           sg1                   240        0       1     .
00023           sg1                   240        0       1     .
00024           sg1                   240        0       1     .

and what's your input?
Looks like you're not just merging columns - you're also merging rows as well....

When i run a system specific command, i get the input which is attached. I add

<system command> | sed '1,2d' | sed '$d' | awk -F" " '{ if (NF == 6) print $1, $2, $3, $4, $5, $6; if (NF == 5) print $1, $2, $3, $4, $5; }' 

to get the output.

this isn't helping - I'll ask again. Can you provide a sample (made up if it's confidential) representative set of input data you're trying to "merge". And a desired matching output.

I've made an assumption:
each record consists a 6 field row followed by an optional 5 field row

awk -F" " '
NF==6 {
   if(getline nl) {
     if(split(nl, v, " ") == 5) for(i=2;i<7;i++) $i = $i","v[i-1]
     else $0=$0"\n"nl
  }
}
1' infile
1 Like

Thank you Chubler for the help.