Format the contents of a file

I have a file/output like below from a script.


gg-qa2 5201  qavm01.dev.example.com
gg-qa2 5201  qavm02.dev.example.com
gg-qa2 5201  qavm03.dev.example.com

is there anyway I can format it like below. Actually any scripting method is fine.

gg-qa2 5201 qavm01.dev.example.com,qavm02.dev.example.com,qavm03.dev.example.com

try below

awk '{if (a[$1FS$2FS]) {a[$1FS$2FS] =a[$1FS$2FS]","$3} else {a[$1FS$2FS]=$3}} END {for (key in a) { print key a[key]}}' testfile
1 Like

A bit altered coding:

awk '{key=$1FS$2; a[key]=((key in a) ? a[key]","$3 : $3)} END {for (key in a) {print key, a[key]}}' testfile 
2 Likes
awk '{if (!b[$1,$2]++) {if (a) print a; a=$0} else {a=a "," $NF}}  END { print a } ' infile
1 Like

Is there a way to print as below for the same input?

gg-qa2 /apps/5201/conf qavm01.dev.example.com,qavm02.dev.example.com,qavm03.dev.example.com

Where would that info come from? A constant? A parameter?

a constant

Then, prepend any of the suggestions in this thread with

$2="/apps/" $2 "/conf";