Convert to csv using sed.
Hi,
I am having a log file with below as output (Package names and the versions):- ql23xx-firmware-3.03.27-3.1.el6.noarch 3.03.27 cryptsetup-luks-1.2.0-7.el6.x86_64 1.2.0 perl-Test-Harness-3.17-136.el6.x86_64 3.17 openssh-clients-5.3p1-94.el6.x86_64 5.3p1 enchant-1.5.0-4.el6.x86_64 1.5.0 perl-Module-Build-0.3500-136.el6.x86_64 0.3500 zip-3.0-1.el6.x86_64 3.0 perl-PPI-1.206-4.el6.noarch 1.206 fontpackages-filesystem-1.41-1.1.el6.noarch 1.41 I need to get the output into csv format like this:- Package name,version. i.e. zip-3.0-1.el6.x86_64, 3.0 So each line will have package name and version seperated by a comma. Any idea how can we achieve this using sed or awk? |
What have you tried so far?
|
Yep ... plenty of ideas :) As above, you show us what you have and we are more than willing to help
|
you could even just replace the hidden "new line" with a comma
and do you HAVE to use the terminal ? gedit and vi and emacs can do this |
Quote:
http://www.linuxquestions.org/questi...pt-4175524161/ http://www.linuxquestions.org/questi...on-4175524568/ http://www.linuxquestions.org/questi...ts-4175523580/ Read the "Question Guidelines" link in my posting signature...and please stop asking for handouts, and show some effort of your own. |
Sure, below is what I tried (Sorry for not including the same before in my post). I am just looking better way of doing this so that I can learn from this forum. I always try to research first and then use this forum to enhance my scripting skills.
for i in $(rpm -qa); do echo $i $(rpm -qi $i| grep Version | awk '{print $3}') >> $HOME/MyLog/installed_packages_temp_$HOSTNAME.csv; done sleep 1 cat $HOME/MyLog/installed_packages_temp_$HOSTNAME.csv | awk '{print $1,",",$2}' >> $HOME/MyLog/installed_packages_$HOSTNAME.csv Thanks. |
Simplest to merely add the comma to the echo on the first command line, then it's all done there. Like this
Code:
for i in $(rpm -qa); do echo $i","$(rpm -qi $i| grep Version | awk '{print $3}') >> $HOME/MyLog/installed_packages_temp_$HOSTNAME.csv; done You can get rid of the grep and do that in the awk if you want to clean it up a little. Lots of other ways of doing it primarily in awk as well. |
but i have to ask
the version number is in the rpm name so why do you need it a second time ? |
Hi Guys,
Thanks for prompt response. John, version number is required for reporting per se only. This is the format of report our client needs to receive for all packages installed on unix hosts. I agree that the info is duplicated as we have the version number available in the package name itself. Regards, Vipin |
Looking at the manpage for rpm, you see there are a lot of reporting options. If you can convince your client to accept the package name and version, this could all be done in just one rpm command
Code:
rpm -qa --qf %{NAME}", "%{VERSION}"\n" |
Quote:
Quote:
|
Hi TBOne,
I really don't understand the reason behind your posts being so rude. Sorry. I have done scripting around few years back and now I need to do that back again due to some project. I use this forum to get a solution which could be better than I adopt - Its true that I should have posted the solution which I had discovered but I never expected anyone to help me with the complete script. It was just to get a better and efficient solution as experienced developers live on this forum. Hope this helps, Thanks. |
You can use RPM Query Formatting to do this like so:
Code:
echo "NAME,VERSION" > output.csv; rpm -qa --queryformat "%{NAME},%{VERSION}\n" >> output.csv Code:
rpm --querytags |
Quote:
Quote:
Quote:
|
All times are GMT -5. The time now is 07:46 AM. |