views:

108

answers:

3

Hi,

I have a file with many lines in it. One of them look like :

set auto_upgrade {1 2 3 4}

The list can contain more numbers.

Now, in a shell script I want to look into this file and find out what is the last number in the list. In the example above, it should find 4. I have a regular expression to find this :

set auto_upgrade {\([0-9] \)*\([0-9]\{1,\}\)}

The second group contains what I need. However, how do I use this regular expression ? I tried sed but it returns the full file with a substition. I just want the second group.

Thanks in advance, Barth

A: 

Something like

cat filename | perl -pe 's/^.* (\d+)\}/$1/'

should work.

Brian L
A: 
sed 's/.*\([0-9]\+\)}/\1/g'

The "\1" in the replacement section matches the group. i.e. (ignoring the escapes and quotes)

sed s/pattern1(pattern2)/\1/g

will return the matched part of pattern2

Steve B.
+3  A: 

First, start sed in quiet mode: it should not print anything unless told to do so (that's the flag -n). Then print only if a substitution has been made (that's the pattern modifier p)

sed -n 's/set auto_upgrade {\([0-9] \)*\([0-9]\{1,\}\)}/\2/p'
soulmerge