Sed question

Avi Greenbury lists at avi.co
Fri Sep 23 23:31:36 UTC 2011


Koh Choon Lin wrote:

> Hi
> 
> > Can you help me with a sed command? I have a string as follows;
> > FileVersion:     14.0.4756.1000
> > And I want to remove everything before the 1, leaving;
> > 14.0.4756.1000
> > I thought that the following should do it but it doesn’t appear to
> > work. Any ideas what I should be using?
> > sed "/^FileVersion: */!d; s///;q"
> > (This is using SED for windows, but as far as I can see the syntax
> > is the same).

These two are different. You first state that what's important is that
you remove everything before the 1, you then go on to concoct a sed
expresion that uses the string 'FileVersion'.

I know this reeks of pedantry, but it's _really_ hard to come up with
a regex when we don't really know what wants matching. With that
initial explanation, there's several ways we might match it:

* The second group of not-whitespace
* The last block of numbers and decimal pojnts
* Everything after a colon followed by (optional?) whitespace
* The last 14 characters/numbers/not-whitespace)
* Everything including and follow the '1'

Personally, the obvious way to do that to me is to use awk:

echo "FileVersion: 14.0.4756.1000" | awk '{print $2}'

assuming I've guessed correctly what you're after. For this sort of
thing, awk's more simple than sed - it's instantly obvious (to anyone
familiar with awk) what that does, and there's less scope for typing
error.

Can you give us more info on what the other lines are likely to look
like, particularly any edge cases?

-- 
Avi




More information about the ubuntu-users mailing list