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?
11-22-2011, 02:01 AM
Pandu Poluan
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/'
11-22-2011, 02:03 AM
Pandu Poluan
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,
11-22-2011, 02:26 AM
Adam Carter
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
11-22-2011, 02:39 AM
Joseph Davis
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.
11-22-2011, 02:47 AM
Joseph Davis
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.
11-22-2011, 03:49 AM
Benjamin Lee
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?
> 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!
11-22-2011, 04:28 AM
Pandu Poluan
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
11-22-2011, 08:40 AM
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?