sed: replace only the first range of numbers

Posted by Marit Hoen on Super User See other posts from Super User or by Marit Hoen
Published on 2012-09-10T11:11:06Z Indexed on 2012/12/16 17:08 UTC
Read the original article Hit count: 202

Filed under:
|

Imagine I have an input file like this:

INSERT INTO video_item_theme VALUES('9', '29');
INSERT INTO video_item_theme VALUES('19', '312');
INSERT INTO video_item_theme VALUES('414', '1');

And I wish to add 10000 to only the first range of numbers, so I end up with something like this:

INSERT INTO video_item_theme VALUES('10009', '29');
INSERT INTO video_item_theme VALUES('10019', '312');
INSERT INTO video_item_theme VALUES('10414', '1');

My approach would be to prefix "1000" to one digit numbers, "100" Something like...:

sed 's/[0-9]\{2\}/10&/g' 

... isn't very helpful, since it changes each occurance of two numbers, not only in the first occurance of numbers:

INSERT INTO video_item_theme VALUES('9', '10029');
INSERT INTO video_item_theme VALUES('10019', '100312');
INSERT INTO video_item_theme VALUES('100414', '1');

© Super User or respective owner

Related posts about regex

Related posts about sed