Shell script ouput conversion


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Shell script ouput conversion
# 1  
Old 02-04-2020
Shell script ouput conversion

Hi All,


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


Code:
#!/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 :

Code:
{"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 :

Code:
"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 .

Moderator's Comments:
Mod Comment Please use code tags for future post

Last edited by Chubler_XL; 02-04-2020 at 09:52 PM.. Reason: Added missing code tags
# 2  
Old 02-04-2020
try the following:

Code:
#!/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

# 3  
Old 02-05-2020
Without seeing / knowing pkginfo's output, couldn't the
Code:
egrep '(BASEDIR|NAME|VERSION)' | awk '{print}' > /tmp/cp1

just be dropped without affecting the result? (aside: Esp. the awk '{print}')
# 4  
Old 02-05-2020
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.

Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!



Current output :


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



Required output :



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


Last edited by vgersh99; 02-05-2020 at 03:45 PM.. Reason: code tags, please!
# 5  
Old 02-05-2020
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:

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


or try
Code:
                {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/).
# 6  
Old 02-05-2020
How about this simple and straightforward (and untested as well!) version:

Code:
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=":"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

powershell script to unix shell script conversion.

Here is a powershell script to use restful API to create ticket in our ticketing tool. Can anyone please convert it to a shell script sothat, I can run it in Unix servers, below is the code: $body = @{ Customer= ''test' Summary= 'test summary' Impact= '4-Minor/Localized' ... (2 Replies)
Discussion started by: pandeybhavesh18
2 Replies

2. Shell Programming and Scripting

Shell Script has different ouput via cron vs when run Manually

Hello Lads, I deployed a script on my mac to start and stop EC2 instances on AWS console. The script when started manually on the terminal does the expected stop and start. Problem is when i try to schedule it on a cron, it fails to recognize the AWS Keys which i set up as ENV variable by... (2 Replies)
Discussion started by: Irishboy24
2 Replies

3. Shell Programming and Scripting

Batch to shell script conversion

Hi All, I have a small tool which is currently configured in batch scripts only. But my need is to run it on Linux platform, so I have been trying to convert a batch script to shell script. below is the batch script: @echo off IF "%1"== "" GOTO ARGERR REM UPDATE THESE PROPERTIES TO... (2 Replies)
Discussion started by: sukhdip
2 Replies

4. Shell Programming and Scripting

Conversion batch shell script

while converting batch file to shell script ...dis command is ther i dunno how to change...can anyone knws how to change into shell script rm !(D:\temp\XX.txt) (3 Replies)
Discussion started by: monisha
3 Replies

5. Shell Programming and Scripting

Shell Script for Epoch Time Conversion

Hi there I came across this script online to convert Epoch time to proper date format, but I am receiving the following error Also, I have HISTTIMEFORMAT set in user's .profile so that their history output shows time stamps. Additionally I have changed their .history location to a dedicated... (9 Replies)
Discussion started by: hedkandi
9 Replies

6. Shell Programming and Scripting

Shell script for CSV conversion

thanks for allowing me join your forum i have an output of linux command "who" which provides following details..... CURRENT USER/ACCT INFO 17:31:36 up 4:49, 4 users, load average: 0.03, 0.04, 0.00 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root :0 - 12:59 ?xdm? 4:54 0.02s /bin/sh /usr/bi... (1 Reply)
Discussion started by: ayyappancheta
1 Replies

7. Shell Programming and Scripting

Limit ouput file on a shell script

I have this one::) doing jstack on JVM proccess. #!/bin/ksh # ---------------------------------------------------- # capture_jstack.sh <INTERVAL> <COUNT> # run jstack and capture output to a log file #----------------------------------------------------- # # scripts and logs are stored... (3 Replies)
Discussion started by: pointer
3 Replies

8. AIX

Need timestamp conversion shell script !!

Can anyone provide me with a ksh or bash script which will accept a timestamp (format is YYYY-MM-DD-HH24.Mi.Ss) and time offset (in hours). The output will be (timestamp passed - time offset passed deducted from it) in the same YYYY-MM-DD-HH24.Mi.Ss format. Basically I am trying to convert the... (1 Reply)
Discussion started by: shibajighosh
1 Replies

9. Shell Programming and Scripting

Help me with file conversion [Shell Script]

Hello Group, I request your help with a shell script for filter an ascii file (this is small piece of the file): 09/24/2009,00:00,1.0268,1.0268,1.0249,1.0250,518 09/24/2009,01:00,1.0251,1.0261,1.0249,1.0259,424 09/24/2009,02:00,1.0258,1.0272,1.0258,1.0269,372... (3 Replies)
Discussion started by: csierra
3 Replies

10. Shell Programming and Scripting

shell script conversion to binary

My question is: i have a script called getevent to run i just call ./getevent can i convert this to make it binary executable and not letting my clients open it and see the code.??:( I am using Solaris 8. (3 Replies)
Discussion started by: bcheaib
3 Replies
Login or Register to Ask a Question