Shell script ouput conversion

Hi All,

I am trying to print all the packages info in solaris 11 using below script.

#!/usr/bin/env bash

pkginfo -l | egrep '(BASEDIR|NAME|VERSION)' | awk '{print}' > /tmp/cp1

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

But i am getting below output.

Current 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":"/" ||}

But i am expecting required output as below :

"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":"/" ||

Can someone please help me on this issue .

try the following:

#!/usr/bin/env bash
  
pkginfo -l | egrep '(BASEDIR|NAME|VERSION)' | awk '{print}' > /tmp/cp1

/usr/bin/nawk -F: '
                {for (i=1; i<=NF; i++)  {gsub (/^ *| *$/, "", $i)
                                         $i = "\"" $i "\""
                                        }
                }
/NAME/          {printf "%s,", $0}
/VERSION/       {printf "%s,", $0}
/BASEDIR/       {printf "%s || ", $0 }
END {printf "\n"}
' OFS=":" /tmp/cp1

Without seeing / knowing pkginfo 's output, couldn't the

egrep '(BASEDIR|NAME|VERSION)' | awk '{print}' > /tmp/cp1

just be dropped without affecting the result? (aside: Esp. the awk '{print}' )

Thanks Chubler_XLand Rudi C.

I am able to print the output now.

But when i use lowercase column names like name, version and basedir it's not displaying correct output.
Can someone please help on this issue.

Current output :

 NAME:  LSI MegaRAID SAS2.0 HBA Driver
   VERSION:  11.11,REV=2009.11.11
   BASEDIR:  /

Required output :

 name:  LSI MegaRAID SAS2.0 HBA Driver
   version:  11.11,REV=2009.11.11
   basedir:  /

You should have a look at the awk tolower() function.

In your case you want to replace field #1 with a lowercase version of it's self something like this:

/NAME/ { $1=tolower($1); printf "%s" $0 }

or try

                {for (i=1; i<=NF; i++)  {gsub (/^ *| *$/, "", $i)
                                         $i = "\"" (i == 1 ? tolower($1) : $i) "\""
                                        }
                }

If you go with the above method, remember the expressions following will now be looking for lowercase a version (ie /basedir/ instead of /BASEDIR/ ).

How about this simple and straightforward (and untested as well!) version:

pkginfo -l |
/usr/bin/nawk -F: '
                                {for (i=1; i<=NF; i++)  {gsub (/^ *| *$/, "", $i)
                                                         $i = "\"" $i "\""
                                                        }
                                }

/NAME/    || /name/ ||
/VERSION/ || /version/          {printf "%s,", $0}
/BASEDIR/ || /basedir/          {printf "%s || ", $0 }
END                             {printf "\n"}
' OFS=":"