Convert rows to column and print output in required format

Hi All,

i am trying to print the solaris 11 packages in below required format, But i am unable to do that.

Current ouput :

root@abc# pkginfo -l | egrep '(BASEDIR|NAME|VERSION)' | awk '{print}' 
      NAME:  QLogic 570x/571x Gigabit Ethernet Driver
   VERSION:  11.11,REV=2009.11.11
   BASEDIR:  /
      NAME:  QLogic 57xxx 10/20GbE NIC Driver
   VERSION:  11.11,REV=2009.11.11
   BASEDIR:  /
      NAME:  HP Smart Array HBA Driver 2.4.4.1
   VERSION:  11.11,REV=2009.11.11
   BASEDIR:  /
      NAME:  cas_migrateconf - The migrateconf class action script for CSW packages
   VERSION:  1.50,REV=2015.01.17
   BASEDIR:  /
      NAME:  cas_preserveconf - The preserveconf class action script for CSW packages
   VERSION:  1.50,REV=2015.01.17
   BASEDIR:  /
      NAME:  common - common files and dirs for CSW packages
   VERSION:  1.5,REV=2010.12.11
   BASEDIR:  /
      NAME:  ggettext_data - GNU locale utilities, translation files
   VERSION:  0.19.8,REV=2016.09.08
   BASEDIR:  /
      NAME:  iconv_stub - Transitional package. Content moved to CSWlibcharset1 CSWlibiconv2
   VERSION:  1.14,REV=2011.08.08
   BASEDIR:  /
      NAME:  jq - A lightweight and flexible command-line JSON processor.
   VERSION:  1.4,REV=2014.09.07
   BASEDIR:  /
      NAME:  libbz2_1_0 - Compression library, libbz2.so.1.0
   VERSION:  1.0.6,REV=2011.08.18
   BASEDIR:  /
      NAME:  libcharset1 - GNU iconv charset library, libcharset.so.1
   VERSION:  1.14,REV=2011.08.07
   BASEDIR:  /
      NAME:  libiconv2 - GNU iconv library, libiconv.so.2
   VERSION:  1.14,REV=2011.08.07
   BASEDIR:  /
      NAME:  libidn11 - GNU IDN library, libidn.so.11
   VERSION:  1.33,REV=2016.09.05
   BASEDIR:  /

Desired output :

{"NAME":  "QLogic 570x/571x Gigabit Ethernet Driver",
   "VERSION":  "11.11,REV=2009.11.11",
   "BASEDIR":  "/"},
 { "NAME":  "QLogic 57xxx 10/20GbE NIC Driver",
   "VERSION":  "11.11,REV=2009.11.11",
   "BASEDIR":  "/"}

i tried to convert rows to column but with awk filter i am not able to print required fields data
Can someone please help me on this issue.

1)paste - - - < out
2)with awk syntax i am unable to print entire package name.

How about

awk -F: '
                {for (i=1; i<=NF; i++)  {gsub (/^ *| *$/, "", $i)
                                         $i = "\"" $i "\""
                                        }
                }
/NAME/          {print "{" $0 ","}
/VERSION/       {print     $0 ","}
/BASEDIR/       {print     $0 "}"}
' OFS=":" file

Hi Rudic,

Thanks for the reply, But when i am trying to executing the script it's repeating each line output three times as well as it's missing double quotes in some places

{"NAME":"QLogic 570x/571x Gigabit Ethernet Driver         VERSION":"11.11,REV=2009.11.11          BASEDIR":"/",
"NAME":"QLogic 570x/571x Gigabit Ethernet Driver           VERSION":"11.11,REV=2009.11.11          BASEDIR":"/",
"NAME":"QLogic 570x/571x Gigabit Ethernet Driver           VERSION":"11.11,REV=2009.11.11          BASEDIR":"/"}

Desired ouput :

{"NAME":"QLogic 570x/571x Gigabit Ethernet Driver",         "VERSION":"11.11,REV=2009.11.11",        "BASEDIR":"/"}

if on Solaris, use either nawk or /usr/xpg4/bin/awk

thanks vgersh99 i am using nawk now but i am getting each line output three times as well as it's missing double quotes in some places

{"NAME":"QLogic 570x/571x Gigabit Ethernet Driver VERSION":"11.11,REV=2009.11.11 BASEDIR":"/",
"NAME":"QLogic 570x/571x Gigabit Ethernet Driver VERSION":"11.11,REV=2009.11.11 BASEDIR":"/",
"NAME":"QLogic 570x/571x Gigabit Ethernet Driver VERSION":"11.11,REV=2009.11.11 BASEDIR":"/"}

Desired ouput :

{"NAME":"QLogic 570x/571x Gigabit Ethernet Driver", "VERSION":"11.11,REV=2009.11.11", "BASEDIR":"/"}

Strange...
Given your sample input in post#1 and using RudiC's code, I get:

{"NAME":"QLogic 570x/571x Gigabit Ethernet Driver",
"VERSION":"11.11,REV=2009.11.11",
"BASEDIR":"/"}
{"NAME":"QLogic 57xxx 10/20GbE NIC Driver",
"VERSION":"11.11,REV=2009.11.11",
"BASEDIR":"/"}
{"NAME":"HP Smart Array HBA Driver 2.4.4.1",
"VERSION":"11.11,REV=2009.11.11",
"BASEDIR":"/"}
{"NAME":"cas_migrateconf - The migrateconf class action script for CSW packages",
"VERSION":"1.50,REV=2015.01.17",
"BASEDIR":"/"}
{"NAME":"cas_preserveconf - The preserveconf class action script for CSW packages",
"VERSION":"1.50,REV=2015.01.17",
"BASEDIR":"/"}
{"NAME":"common - common files and dirs for CSW packages",
"VERSION":"1.5,REV=2010.12.11",
"BASEDIR":"/"}
{"NAME":"ggettext_data - GNU locale utilities, translation files",
"VERSION":"0.19.8,REV=2016.09.08",
"BASEDIR":"/"}
{"NAME":"iconv_stub - Transitional package. Content moved to CSWlibcharset1 CSWlibiconv2",
"VERSION":"1.14,REV=2011.08.08",
"BASEDIR":"/"}
{"NAME":"jq - A lightweight and flexible command-line JSON processor.",
"VERSION":"1.4,REV=2014.09.07",
"BASEDIR":"/"}
{"NAME":"libbz2_1_0 - Compression library, libbz2.so.1.0",
"VERSION":"1.0.6,REV=2011.08.18",
"BASEDIR":"/"}
{"NAME":"libcharset1 - GNU iconv charset library, libcharset.so.1",
"VERSION":"1.14,REV=2011.08.07",
"BASEDIR":"/"}
{"NAME":"libiconv2 - GNU iconv library, libiconv.so.2",
"VERSION":"1.14,REV=2011.08.07",
"BASEDIR":"/"}
{"NAME":"libidn11 - GNU IDN library, libidn.so.11",
"VERSION":"1.33,REV=2016.09.05",
"BASEDIR":"/"}

Also, please start using code tags before acquiring too many infractions.....

1 Like

Very strange. Did you use a one-to-one copy of my proposal? If yes: Looks like line terminators are missing. Can you post each line's NF ? On top, pls post input data and a hexdump of (a decent, small, representative subset of) it.

yes
now i am getting required ouput.
Thanks so much for your help Rudic and vgersh99 .