Weblog entry #22 for Utumno

Sed advice
Posted by Utumno on Mon 12 Feb 2007 at 02:58
Tags:
I've got a line that looks like this

(crap)7-digit-number-1(crap)7-digit-number-2(crap)....7-digit-number-n(crap)

and I would like to parse out the 1st 7-digit number out of this with sed. My problem is that sed, being greedy, always matches the last number, i.e. if I write
echo line | sed -n "s/.*\([0-9]\{7\}\).*/\1/p"

I am going to get the last 7-digit number rather than the first. I dont know how many 7-digit numbers are there in my line.

Anyone could shed some light on this?

 

Comments on this Entry

Posted by Anonymous (61.246.xx.xx) on Mon 12 Feb 2007 at 04:25
Maybe http://www.grymoire.com/Unix/Sed.html#uh-4 helps.

The approach to me seems to be to realize * is greedy, so logically keep away from the front.

BTW, if you occasionally use sed, you really should bookmark that site. Excellent introductory coverage of sed.

PJ

[ Parent | Reply to this comment ]

Posted by mcortese (213.70.xx.xx) on Mon 12 Feb 2007 at 17:45
[ Send Message | View Weblogs ]
Try splitting the command into two:
cat line | sed -e "s/\([0-9]\{7\}\).*/\1/" -e "s/.*\([0-9]\{7\}\)/\1/"
M.

[ Parent | Reply to this comment ]

Posted by busfault (69.205.xx.xx) on Thu 15 Feb 2007 at 15:05
[ Send Message | View Weblogs ]
I know you are using sed but this could be also easily done using grep
grep -o "[0-9]\{7\}"

pyrrhic@Valhalla:~$ cat sedtest
asdasdfasdfasdf1234567dsdgfdsgf2345671dsfgsdfgsdfg3456712sdfgsdfg dsfg

pyrrhic@Valhalla:~$ cat sedtest |grep -o "[0-9]\{7\}"
1234567
2345671
3456712

adding a |head -1 would give you the first result.

[ Parent | Reply to this comment ]

Posted by Utumno (61.229.xx.xx) on Sat 17 Feb 2007 at 19:17
[ Send Message | View Utumno's Scratchpad | View Weblogs ]
Excellent advice, thanks.

[ Parent | Reply to this comment ]

User Login

Username:

Password:

[ Advanced Login ]

Register Account

Quick Site Search