LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-13-2014, 05:32 AM   #1
vipinsqa
Member
 
Registered: Oct 2014
Posts: 40

Rep: Reputation: Disabled
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?
 
Old 11-13-2014, 06:47 AM   #2
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 979
Blog Entries: 2

Rep: Reputation: 235Reputation: 235Reputation: 235
What have you tried so far?
 
Old 11-13-2014, 09:42 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,255

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
Yep ... plenty of ideas As above, you show us what you have and we are more than willing to help
 
Old 11-13-2014, 04:32 PM   #4
John VV
LQ Muse
 
Registered: Aug 2005
Location: A2 area Mi.
Posts: 16,829

Rep: Reputation: 2409Reputation: 2409Reputation: 2409Reputation: 2409Reputation: 2409Reputation: 2409Reputation: 2409Reputation: 2409Reputation: 2409Reputation: 2409Reputation: 2409
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

Last edited by John VV; 11-13-2014 at 04:34 PM.
 
Old 11-13-2014, 05:05 PM   #5
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,956

Rep: Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693
Quote:
Originally Posted by vipinsqa View Post
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.
 
Old 11-13-2014, 09:57 PM   #6
vipinsqa
Member
 
Registered: Oct 2014
Posts: 40

Original Poster
Rep: Reputation: Disabled
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.
 
Old 11-13-2014, 11:35 PM   #7
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 14,848

Rep: Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823
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.

Last edited by syg00; 11-13-2014 at 11:36 PM.
 
Old 11-13-2014, 11:46 PM   #8
John VV
LQ Muse
 
Registered: Aug 2005
Location: A2 area Mi.
Posts: 16,829

Rep: Reputation: 2409Reputation: 2409Reputation: 2409Reputation: 2409Reputation: 2409Reputation: 2409Reputation: 2409Reputation: 2409Reputation: 2409Reputation: 2409Reputation: 2409
but i have to ask
the version number is in the rpm name
so
why do you need it a second time ?
 
Old 11-13-2014, 11:49 PM   #9
vipinsqa
Member
 
Registered: Oct 2014
Posts: 40

Original Poster
Rep: Reputation: Disabled
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
 
Old 11-14-2014, 01:39 AM   #10
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 14,848

Rep: Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823
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.
 
1 members found this post helpful.
Old 11-14-2014, 10:22 AM   #11
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,956

Rep: Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693
Quote:
Originally Posted by vipinsqa View Post
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.
 
Old 11-14-2014, 12:18 PM   #12
vipinsqa
Member
 
Registered: Oct 2014
Posts: 40

Original Poster
Rep: Reputation: Disabled
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.
 
Old 11-14-2014, 12:27 PM   #13
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 3,836

Rep: Reputation: 1360Reputation: 1360Reputation: 1360Reputation: 1360Reputation: 1360Reputation: 1360Reputation: 1360Reputation: 1360Reputation: 1360Reputation: 1360
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

Last edited by szboardstretcher; 11-14-2014 at 12:31 PM. Reason: Added link
 
Old 11-14-2014, 12:45 PM   #14
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,956

Rep: Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693
Quote:
Originally Posted by vipinsqa View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Code to convert pdf to csv ibabhelix Linux - Software 5 08-06-2014 04:39 PM
Help me convert this xml file to csv xmrkite Linux - Software 2 06-21-2014 10:48 PM
Using awk/sed to convert linefeed to csv, with some formatting jaykup Programming 1 04-03-2009 06:18 PM
[SOLVED] Convert CSV to Tab imkornhulio Programming 7 11-15-2008 08:20 PM
Convert into CSV file say_hi_ravi Programming 4 07-17-2008 04:25 AM


All times are GMT -5. The time now is 05:21 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration