LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Convert to csv using sed. (https://www.linuxquestions.org/questions/linux-newbie-8/convert-to-csv-using-sed-4175525279/)

vipinsqa 11-13-2014 04:32 AM

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?

linosaurusroot 11-13-2014 05:47 AM

What have you tried so far?

grail 11-13-2014 08:42 AM

Yep ... plenty of ideas :) As above, you show us what you have and we are more than willing to help

John VV 11-13-2014 03:32 PM

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

TB0ne 11-13-2014 04:05 PM

Quote:

Originally Posted by vipinsqa (Post 5269140)
Hi,
I am having a log file with below as output (Package names and the versions):-
Code:

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?

Lots...as others have said, show us what YOU have tried so far. And have you considered actually applying and thinking about anything you've been told in the past, when you've asked such questions? Or do you just want people to spoon-feed you an answer?
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.

vipinsqa 11-13-2014 08:57 PM

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.

syg00 11-13-2014 10:35 PM

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
Add a space in there if you want.
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.

John VV 11-13-2014 10:46 PM

but i have to ask
the version number is in the rpm name
so
why do you need it a second time ?

vipinsqa 11-13-2014 10:49 PM

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

syg00 11-14-2014 12:39 AM

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"
I only played around a little, there may be better options.

TB0ne 11-14-2014 09:22 AM

Quote:

Originally Posted by vipinsqa (Post 5269514)
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.

Then why do we always have to ask you to show what you've done, and why do you continue to just ask people to hand you scripts?
Quote:

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
So if you got that far by yourself, it should be trivial for you to get your desired output.

vipinsqa 11-14-2014 11:18 AM

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.

szboardstretcher 11-14-2014 11:27 AM

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
Additionally, if you want more fields, you can list available RPM fields(tags) with:

Code:

rpm --querytags
Great RPM query resource: http://www.rpm.org/wiki/Docs/QueryFormat

TB0ne 11-14-2014 11:45 AM

Quote:

Originally Posted by vipinsqa (Post 5269753)
Hi TBOne,
I really don't understand the reason behind your posts being so rude.

Because you came here, several times, asking for a handout, and showed no effort of your own. Simple...we aren't going to write your scripts and troubleshoot things for you. Show effort of your own.
Quote:

Sorry. I have done scripting around few years back and now I need to do that back again due to some project.
...and if YOU have to script, then YOU should to it, rather than asking us (again, as you have in the past), to write the script for you. Had you shown some effort in this post, and the ones in the past, the answers may have been different.
Quote:

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.
So then why didn't you post what you wrote, and ask "Can anyone see a better way to do this?", rather than (again) posting what you wanted with nothing of your own to back it up? You've posted SEVERAL threads since you've been here the same way. and have flat out asked for scripts in other threads.


All times are GMT -5. The time now is 07:46 AM.