Convert Rows to Columns Shell Script

Viewed 64

I have data in the below format

APP_OWNER                     : hari
APP_AREA                      : Work:Business Area:AUS
APP_ID                        : 124080

I want the data to be converted to below format.

APP_OWNER,APP_AREA,APP_ID
hari,Work:Business Area:AUS,124080

I can convert one line but how to do it with 3 lines at the same time?

My Attempt with one line

sed '0,/: /s//\n/' test.txt

Regards

1 Answers

You may try this awk solution:

awk -F '[[:blank:]]+:[[:blank:]]+' '{
   v1 = (v1 == "" ? "" : v1 ",") $1
   v2 = (v2 == "" ? "" : v2 ",") $2
}
END {
   print v1 ORS v2
}' file

APP_OWNER,APP_AREA,APP_ID
hari,Work:Business Area:AUS,124080
Related