FAQ Search Today's Posts Mark Forums Read
» Video Reviews

» Linux Archive

Linux-archive is a website aiming to archive linux email lists and to make them easily accessible for linux users/developers.


» Sponsor

» Partners

» Sponsor

Go Back   Linux Archive > Gentoo > Gentoo User

 
 
LinkBack Thread Tools
 
Old 11-22-2011, 01:52 AM
Adam Carter
 
Default sed/awk question

Hi All,

I need to cut a string, which happens to be a filename, using the
first dash that's followed by a numeral, so cut -f 1 -d"-" fails if
the filename has an extra dash. How do i do this?
 
Old 11-22-2011, 02:01 AM
Pandu Poluan
 
Default sed/awk question

On Nov 22, 2011 9:57 AM, "Adam Carter" <adamcarter3@gmail.com> wrote:

>

> Hi All,

>

> I need to cut a string, which happens to be a filename, using the

> first dash that's followed by a numeral, so cut -f 1 -d"-" fails if

> the filename has an extra dash. How do i do this?

>


sed -r -e 's/(.*)-[0-9].*/1/'
 
Old 11-22-2011, 02:03 AM
Pandu Poluan
 
Default sed/awk question

On Nov 22, 2011 10:01 AM, "Pandu Poluan" <pandu@poluan.info> wrote:

>

>

> On Nov 22, 2011 9:57 AM, "Adam Carter" <adamcarter3@gmail.com> wrote:

> >

> > Hi All,

> >

> > I need to cut a string, which happens to be a filename, using the

> > first dash that's followed by a numeral, so cut -f 1 -d"-" fails if

> > the filename has an extra dash. How do i do this?

> >

>

> sed -r -e 's/(.*)-[0-9].*/1/'


You know, that looks familiar... are you trying to get a package name from the list of eix-installed? :-)


Rgds,
 
Old 11-22-2011, 02:26 AM
Adam Carter
 
Default sed/awk question

>> sed -r -e 's/(.*)-[0-9].*/1/'
>
> You know, that looks familiar... are you trying to get a package name from
> the list of eix-installed? :-)

No - its non-gentoo. In this case it hasn't worked

$ echo net-snmp-5.3.2.2-5.cp843034001.i386.rpm | sed -r -e 's/(.*)-[0-9].*/1/'
net-snmp-5.3.2.2
 
Old 11-22-2011, 02:39 AM
Joseph Davis
 
Default sed/awk question

I'd use sed and the regex "-[0-9]" to delimit the field


foo=`echo '123--bad-2xyz-3--' | sed -r -e "s/-[0-9].*//"`
echo $foo
123--bad


Helpful?

Adam Carter wrote:

Hi All,

I need to cut a string, which happens to be a filename, using the
first dash that's followed by a numeral, so cut -f 1 -d"-" fails if
the filename has an extra dash. How do i do this?




--
University of Houston, Cougar Card services support.
 
Old 11-22-2011, 02:47 AM
Joseph Davis
 
Default sed/awk question

Oh, and you can get the other end next by


foo2=`echo '123--bad-2xyz-3--' | sed -r -e "s/$foo//"`
echo $foo2
-2xyz-3--

Joseph Davis wrote:

I'd use sed and the regex "-[0-9]" to delimit the field


foo=`echo '123--bad-2xyz-3--' | sed -r -e "s/-[0-9].*//"`
echo $foo
123--bad


Helpful?

Adam Carter wrote:

Hi All,

I need to cut a string, which happens to be a filename, using the
first dash that's followed by a numeral, so cut -f 1 -d"-" fails if
the filename has an extra dash. How do i do this?






--
University of Houston, Cougar Card services support.
 
Old 11-22-2011, 03:49 AM
Benjamin Lee
 
Default sed/awk question

On 11/21/2011 06:52 PM, Adam Carter wrote:
> Hi All,
>
> I need to cut a string, which happens to be a filename, using the
> first dash that's followed by a numeral, so cut -f 1 -d"-" fails if
> the filename has an extra dash. How do i do this?

Here's a Bourne parameter expansion:

blee@eclipse ~ $ foo="net-snmp-5.3.2.2-5.cp843034001.i386.rpm"
blee@eclipse ~ $ echo ${foo%%-[0-9]*}
net-snmp


--
Benjamin Lee
http://www.b1c1l1.com/
 
Old 11-22-2011, 03:53 AM
Adam Carter
 
Default sed/awk question

> I'd use sed and the regex "-[0-9]" to delimit the field
>
>
> foo=`echo '123--bad-2xyz-3--' | sed -r -e "s/-[0-9].*//"`
> echo $foo
> 123--bad
>
>
> Helpful?

Perfect - thanks!
 
Old 11-22-2011, 04:28 AM
Pandu Poluan
 
Default sed/awk question

On Tue, Nov 22, 2011 at 10:26, Adam Carter <adamcarter3@gmail.com> wrote:
>>> sed -r -e 's/(.*)-[0-9].*/1/'
>>
>> You know, that looks familiar... are you trying to get a package name from
>> the list of eix-installed? :-)
>
> No - its non-gentoo. In this case it hasn't worked
>
> $ echo net-snmp-5.3.2.2-5.cp843034001.i386.rpm | sed -r -e 's/(.*)-[0-9].*/1/'
> net-snmp-5.3.2.2
>

Ah, yes. sed's greedy regex again messes up the plan >.<

Here's an alternative:

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

Basically, the regex above tries to (greedily) match "dash followed by
a digit followed by zero or more (any) characters"... then deletes the
match. Thus leaving the front part of the string untouched.

(And yes, this time I've tested the sed incantation)

Rgds,
--
FdS Pandu E Poluan
~ IT Optimizer ~

*• LOPSA Member #15248
*• Blog : http://pepoluan.tumblr.com
*• Linked-In : http://id.linkedin.com/in/pepoluan
 
Old 11-22-2011, 08:40 AM
 
Default sed/awk question

Pandu Poluan <pandu@poluan.info> wrote:

> On Tue, Nov 22, 2011 at 10:26, Adam Carter <adamcarter3@gmail.com> wrote:
> >>> sed -r -e 's/(.*)-[0-9].*/1/'
> >>
> >> You know, that looks familiar... are you trying to get a package name from
> >> the list of eix-installed? :-)
> >
> > No - its non-gentoo. In this case it hasn't worked
> >
> > $ echo net-snmp-5.3.2.2-5.cp843034001.i386.rpm | sed -r -e 's/(.*)-[0-9].*/1/'
> > net-snmp-5.3.2.2
> >
>
> Ah, yes. sed's greedy regex again messes up the plan >.<
>
> Here's an alternative:
>
> sed -r -e 's/-[0-9].*//'

Nust a note: sed has no option -r and 's/(.*)-[0-9].*/1/' is a "garbled"
command. A corrected version would be 's/(.*)-[0-9].*/1/'

So the main question is: why do you use a non-existing option?

Jrg

--
EMail:joerg@schily.isdn.cs.tu-berlin.de (home) Jrg Schilling D-13353 Berlin
js@cs.tu-berlin.de (uni)
joerg.schilling@fokus.fraunhofer.de (work) Blog: http://schily.blogspot.com/
URL: http://cdrecord.berlios.de/private/ ftp://ftp.berlios.de/pub/schily
 

Thread Tools




All times are GMT. The time now is 11:20 PM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright 2007 - 2008, www.linux-archive.org