LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-06-2014, 07:22 PM   #1
socalheel
Member
 
Registered: Oct 2012
Location: Raleigh, NC
Distribution: CentOS / RHEL
Posts: 158

Rep: Reputation: 3
extract just the package name without version number from .txt file


i have a file with about 900 lines of packages that are installed on my server. i need to strip all the version information off, and give me only the package name.

here is a snipit

Code:
wbt-virtual-server-theme-8.8-1.noarch
webalizer-2.21_02-3.3.el6.x86_64
webmin-1.680-1.noarch
wget-1.12-1.11.el6_5.x86_64
which-2.19-6.el6.x86_64
wireless-tools-29-5.1.1.el6.x86_64
words-3.0-17.el6.noarch
xdg-utils-1.0.2-17.20091016cvs.el6.noarch
xml-common-0.6.3-32.el6.noarch
xmlrpc-c-1.16.24-1210.1840.el6.x86_64
xmlrpc-c-client-1.16.24-1210.1840.el6.x86_64
xorg-x11-drv-ati-firmware-7.1.0-3.el6.noarch
xorg-x11-font-utils-7.2-11.el6.x86_64
xz-4.999.9-0.3.beta.20091007git.el6.x86_64
xz-libs-4.999.9-0.3.beta.20091007git.el6.x86_64
xz-lzma-compat-4.999.9-0.3.beta.20091007git.el6.x86_64
yp-tools-2.9-12.el6.x86_64
ypbind-1.20.4-30.el6.x86_64
yum-3.2.29-43.el6_5.noarch
yum-metadata-parser-1.1.2-16.el6.x86_64
yum-plugin-security-1.1.30-17.el6_5.noarch
yum-rhn-plugin-0.9.1-49.el6.noarch
yum-utils-1.1.30-17.el6_5.noarch
zd1211-firmware-1.4-4.el6.noarch
zip-3.0-1.el6.x86_64
i need only this:
Code:
wbt-virtual-server-theme
webalizer
webmin
wget
which
wireless-tools
words
xdg-utils
xml-common
xmlrpc-c-
xmlrpc-c-client
xorg-x11-drv-ati-firmware
xorg-x11-font-utils
xz
xz-libs
xz-lzma-compat
yp-tools
ypbind
yum
yum-metadata-parser
yum-plugin-security
yum-rhn-plugin
yum-utils
zd1211-firmware
zip
how can i do that?
 
Old 11-06-2014, 07:30 PM   #2
socalheel
Member
 
Registered: Oct 2012
Location: Raleigh, NC
Distribution: CentOS / RHEL
Posts: 158

Original Poster
Rep: Reputation: 3
i mean, i can accomplish this inside vim with multiple " :%s/ //g" commands ... but how could i do this from the command line?
 
Old 11-06-2014, 08:08 PM   #3
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
This is what I use for a package management script I wrote, it seems to work here too, but do some more tests
Code:
cat test | rev | cut -d- -f 1-2 --complement | rev
 
1 members found this post helpful.
Old 11-06-2014, 08:22 PM   #4
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,

looks to me you want to delete everything after the first hyphen that is followed by a number. So:
Code:
sed 's/-[0-9].*//g'
HTH,

Evo2.
 
1 members found this post helpful.
Old 11-06-2014, 08:31 PM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,138

Rep: Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122
Normally I'd say just create some regex equivalent to the vim commands and use sed, but regex being greedy messes that. How about plain old bash parameter substitution ?
Code:
while read line ; do echo ${line%%-[0-9]*} ; done < infile.txt
(presumes you want to lose everything that is after the first -[:digit:])


Last edited by syg00; 11-06-2014 at 08:33 PM. Reason: evo2 post while I was getting coffee :D
 
1 members found this post helpful.
Old 11-06-2014, 08:43 PM   #6
socalheel
Member
 
Registered: Oct 2012
Location: Raleigh, NC
Distribution: CentOS / RHEL
Posts: 158

Original Poster
Rep: Reputation: 3
Quote:
Originally Posted by evo2 View Post
Hi,

looks to me you want to delete everything after the first hyphen that is followed by a number. So:
Code:
sed 's/-[0-9].*//g'
HTH,

Evo2.
oh man that is so freakin simple ... i think when i first saw all the variations and multiple fields followed by a hyphen i got overwhelmed for a second.

exactly what i needed.

thanks dude.
 
  


Reply



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
Which command can extract just number from a line or text file? Xilonen Linux - Newbie 2 07-09-2013 04:48 AM
2 .txt files trying to extract data from master file rhbegin Programming 5 01-04-2012 08:53 AM
extract last few of line data from txt file irwinyeo Programming 7 12-20-2011 09:47 AM
Need to convert a large number of file types from none standard to txt metalme Linux - Newbie 2 09-28-2009 05:46 PM
[Solaris 9] find package version number noir911 Solaris / OpenSolaris 5 08-15-2008 05:00 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:27 PM.

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
Open Source Consulting | Domain Registration