0

demilited file like this

Input1 file1:45764
Input1 file1:878755
Input1 file1: 899787
Input2 file1: 45676
Input2 file1:769678
Input2 file1: 6454764

and I wish to convert them into

Input1 file1:45764, file1:878755, file1: 899787 
Input2 file1:45676, file1:769678, file1: 6454764 

Any guess? Thanks in advance

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274

2 Answers2

1
awk '{b[$1]=b[$1] $2$3"  "}END{for (i in b) print i,b[i]}' inputFile

will produce output as

Input1 file1:45764  file1:878755  file1:899787  
Input2 file1:45676  file1:769678  file1:6454764  

what it does?

{b[$1]=b[$1] $2$3" "} creates an array b appends the second and third column(since there was some spaces betweenfile and value in your example).$2$3 into the array. The array is an associative array indexed by the Inputx where x is 1,2...

that is

b['Input1'] = 'file1:45764  file1:878755  file1:899787'

END block is excecuted at end of input file, input,

for (i in b) print i,b[i]} prints the content of b array

nu11p01n73R
  • 26,397
  • 3
  • 39
  • 52
0

And if you want those commas in the output, try

awk '{if(b[$1])b[$1] = b[$1]", "; b[$1] = b[$1] $2 $3}; END{for(i in b)print i, b[i]}'

or the slightly terser

awk '{b[$1]=b[$1](b[$1]?", ":"")$2$3}END{for(i in b)print i,b[i]}'

output

Input1 file1:45764, file1:878755, file1:899787
Input2 file1:45676, file1:769678, file1:6454764
PM 2Ring
  • 54,345
  • 6
  • 82
  • 182