0

What is the function of [0-9]* in the following statement, which will write all lines that start with an even number, followed by a space, to the file even:

sed -n 's/^[0-9]*[02468] /&/w even' <file

Then, eliminating [0-9]* will have the same function/output. In other words, both statements are the same effect.

sed -n 's/^[02468] /&/w even' <file

Is [0-9]* useless here? I am really confused. Please help me.

1 Answer 1

4

The [0-9]* is not useless; it matches any number (0-9) that show up before the even number range ([02468]). This is to take into account multi-digit even numbers.

For e.g., if you didn't have ^[0-9]* anchored to beginning of your pattern, it would not match:

92
910
308
20

The other pattern you mentioned (/^[02468]/) would only match anything that begins with an even digit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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