Unix Shell - cut

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > October 2006 > cut





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author cut
virus.clear.net.nz

2006-10-27, 7:15 pm

If I have a listing of packages,
such as : -

XML-Parser-2.34.tbz2
bin86-0.16.17.tbz2
libxml2-2.6.26.tbz2
system-tools-backends-1.4.2.tbz2

I want to cut the version number from the listing, leaving only bin86,
libxml2, etc.
I think that cutting a "-" followed by a {0-9} achieve this ??? but cut
only allows for a single delimiter.

How to achieve this ?

thanks

Grant
Ed Morton

2006-10-27, 7:15 pm

virus.clear.net.nz wrote:

> If I have a listing of packages,
> such as : -
>
> XML-Parser-2.34.tbz2
> bin86-0.16.17.tbz2
> libxml2-2.6.26.tbz2 system-tools-backends-1.4.2.tbz2
>
> I want to cut the version number from the listing, leaving only bin86,
> libxml2, etc.
> I think that cutting a "-" followed by a {0-9} achieve this ??? but cut
> only allows for a single delimiter.
>
> How to achieve this ?


It sounds like just:

cut -d- -f1

would do what you want, but I assume there's more to it than you've
explained so far, so perhaps some small, concrete sample input and
expected output would help.

Ed.
Janis Papanagnou

2006-10-27, 7:15 pm

virus.clear.net.nz wrote:
> If I have a listing of packages,
> such as : -
>
> XML-Parser-2.34.tbz2
> bin86-0.16.17.tbz2
> libxml2-2.6.26.tbz2 system-tools-backends-1.4.2.tbz2


Also two entries on one line possible?

> I want to cut the version number from the listing, leaving only bin86,
> libxml2, etc.
> I think that cutting a "-" followed by a {0-9} achieve this ??? but cut
> only allows for a single delimiter.
>
> How to achieve this ?
>
> thanks
>
> Grant


For one entry on each line...

sed 's/-[0-9].*//'


Janis
Janis Papanagnou

2006-10-27, 7:15 pm

Ed Morton wrote:
> virus.clear.net.nz wrote:
>
>
>
> It sounds like just:
>
> cut -d- -f1


I suppose he wants the "-Parser" not to be splitted off from "XML-Parser".

Janis

> would do what you want, but I assume there's more to it than you've
> explained so far, so perhaps some small, concrete sample input and
> expected output would help.
>
> Ed.

Chris F.A. Johnson

2006-10-27, 7:15 pm

On 2006-10-28, virus.clear.net.nz wrote:
> If I have a listing of packages,
> such as : -
>
> XML-Parser-2.34.tbz2
> bin86-0.16.17.tbz2
> libxml2-2.6.26.tbz2
> system-tools-backends-1.4.2.tbz2
>
> I want to cut the version number from the listing, leaving only bin86,
> libxml2, etc.
> I think that cutting a "-" followed by a {0-9} achieve this ??? but cut
> only allows for a single delimiter.
>
> How to achieve this ?


If you have the name in a variable:

pkg=bin86-0.16.17.tbz2
echo ${pkg%-[0-9]*}


If it's in a file or other list, pipe it through sed:

sed 's/\(.*\)-[0-9].*/\1/'

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Michael Tosch

2006-10-30, 7:30 pm

virus.clear.net.nz wrote:
> If I have a listing of packages,
> such as : -
>
> XML-Parser-2.34.tbz2
> bin86-0.16.17.tbz2
> libxml2-2.6.26.tbz2 system-tools-backends-1.4.2.tbz2
>
> I want to cut the version number from the listing, leaving only bin86,
> libxml2, etc.
> I think that cutting a "-" followed by a {0-9} achieve this ??? but cut
> only allows for a single delimiter.
>
> How to achieve this ?
>


This deletes from the last '-' until the end of the line:

sed 's/-[^-]*$//' file

--
Michael Tosch @ hp : com
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com