Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

INPUT

RANDOMSTRING BLA-BLA [111] RANDOMSTRING BLA-BLA

OUTPUT

RANDOMSTRING BLA-BLA [200] RANDOMSTRING BLA-BLA

[111] could contain various numbers with 3 digits: 120 or 300 or 400 or 101, etc.

So all I want ~this:

sed "s/Howtos<\/a> \[111] \//`grep '^<a href="' list.txt | wc -l`/" original.txt

"Regexp'ing the 111" that's the only thing I can't figure out

The output of:

grep '^<a href="' list.txt | wc -l

would be e.g.: 200 in the example

share|improve this question
    
I'm not sure if this is a typo or not, but I think you need to escape both the opening [ and the closing ], if you want those literal chars. –  Telemachus May 8 '11 at 19:32
    
I cannot make out what you are trying to accomplish here. Do you want to rip out the 3 digit numbers for use in something? Do you want to replace them with something else? Is it possible that digits could appear in the random strings before and after the one you want to work with? Are the fields consistent (e.g. Is it always the third space separated item from the left of the string?) –  Caleb May 9 '11 at 9:19
add comment

1 Answer

up vote 2 down vote accepted

regex for 3 digit number: [0-9]{3} So to replace 111 with 200:

sed -E 's/[0-9]{3}/200/g'

you need the -E flag for extended regexs

share|improve this answer
    
Do most versions of sed now support {3}? I was going to suggest \[[0-9][0-9][0-9]\], but it would be nicer to be able to just use {3}. A quick follow-up GNU sed supports {3} if you pass the -r flag. I don't think BSD sed does. (At least OSX's sed doesn't seem to.) –  Telemachus May 8 '11 at 19:29
1  
use sed -E for extended regexs –  ennuikiller May 8 '11 at 19:32
    
Duh, thanks @ennuikiller. –  Telemachus May 8 '11 at 19:33
    
Thank you but I choosed: [[0-9][0-9][0-9]] - because [0-9]{3} doesn't works even with -E :\ /Fedora14/Bash/ –  LanceBaynes May 8 '11 at 19:35
1  
@LanceBaynes If that's GNU sed, try -r instead of -E. (Aren't portability issues fun?) –  Telemachus May 8 '11 at 19:54
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.