Convert Rows to Columns Shell Script

♀尐吖头ヾ 提交于 2021-01-04 05:32:47

问题


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:


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


来源:https://stackoverflow.com/questions/65497666/convert-rows-to-columns-shell-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!