possible sed usage

Sorry about the title. I'm assuming I want sed anyway...

Here's the deal:

I have hundreds of files in a folder and they all are named with the same format.

$NAME-$VERSION-$ARCH-$BUILD

This is Slackwares' /var/log/packages directory BTW...

$BUILD is what I'm focusing on. It could be just "1" or it could be "1foo" or it could be "1foo357" or "1foo357bar"

I'm trying to isolate just the "1" regardless of what comes after it. I came to the conclusion that just identifying the last hyphen and then grabbing the very next number would be the easiest way.

This is what worked great until I started adding custom build tags like foo and foo357:

installed=$(ls /var/log/packages/$NAME-[[:digit:].]* | sed 's/^.*\(.\)$/\1/')

so if $build = 1foo357bar, then the above gives me "r"... The number I want will ALWAYS be after the last hyphen. There is a small chance that the number could reach double digits but it would be rare.

Any ideas? Thanks much for your help. I'm really stumped for some reason.

this sed will do what you wanted..

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

Thanks for the reply but it's not quite what I need.

root@darkstar:~# ls /var/log/packages/glib2-2.18.1-i486-1gnome224jag 
/var/log/packages/glib2-2.18.1-i486-1gnome224jag
root@darkstar:~# installed=$(ls /var/log/packages/glib2-[[:digit:].]* | sed 's/\(-[^-]*-[0-9]\)\(.*\)/\1/g')
root@darkstar:~# echo $installed
/var/log/packages/glib2-2.18.1-i486-1   # installed != 1

root@darkstar:~# ls /var/log/packages/mozilla-firefox-3.0.2-native-1gnome224jag 
/var/log/packages/mozilla-firefox-3.0.2-native-1gnome224jag
root@darkstar:~# installed=$(ls /var/log/packages/mozilla-firefox-[[:digit:].]* | sed 's/\(-[^-]*-[0-9]\)\(.*\)/\1/g')
root@darkstar:~# echo $installed
/var/log/packages/mozilla-firefox-3  # installed != 1

As you can see (I forgot to mention), $NAME can also contain a hyphen sometimes so counting them out in the sed doesn't work half the time. $VERSION should always contain digits but may also contain letters. $ARCH could have either numbers and/or letters (native or i486). And $BUILD can also have both.

If I'm working with a package name like "xpaint-2.7.8.1-i486-2", then my old sed works fine.

root@darkstar:~# installed=$(ls /var/log/packages/xpaint-[[:digit:].]* | sed 's/^.*\(.\)$/\1/')
root@darkstar:~# echo $installed
2  # installed = 2

Thanks again. Much appreciated.

This gives the number(s) after the last hyphen:

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

Regards

Thanks Franklin. It seems to work altho from reading it, I don't know why it's grabbing numerals from $BUILD instead of $VERSION.... :smiley: I thought sed worked from first to last so my understanding from that command is "print the first digits after the first hyphen".

But, it's worked with what I've thrown at it so far. I'll see if I can't wrap my head around it more.

In sed .* is designed to be "greedy" and it always tries to match the longest possible string.

Regards

Yes, that's why I usually prefer using perl or ruby on the command line.

e.g.

ruby -ne 'if /^<i>(.*?):/ then puts $1; end'

Once you are used to perls or ruby's regexs, you can get stuck on sed and grep.

sed -e 's/oldtext/newtext/g'
perl -p -i.bak -e 's/oldtext/newtext/g'

alternative to grep:

That said, there are great things you can do with sed.
Google for "perl one liners" and "sed one liners". http://sed.sourceforge.net/sed1line.txt