-   Linux - Newbie (
-   -   SED - substitution (

carolflb 02-05-2010 07:02 AM

SED - substitution
Hi all,

I'm having a problem by substituting part of this sentence that is save in 'file':


I use the following SED command:

sed -i 's/[^ ]*\([0-9]*\).inTicks_vl([^ ]*);/inTicks_vl\1/' file

so that the sentence looks like this at the end:


I want to move the number, that is part of the first name in the sentence, to the end of the second name in the sentence and erase all the rest.

But what I get with my command is:


so, I believe the command is not recognising that the first name is composed by letters and than by a number... [^ ]* is reading everything and [0-9]* doesn't find anything left...

Would somebody know how to solve this problem? The first name, which has a number at the end, can have random characters...

jschiwal 02-05-2010 07:34 AM

In sed, awk and grep, use .* to match zero or more of any characters. The asterisk matches zero or more of the preceding character. Not zero or more characters. If you want to match a literal period in the LHS, then use "\.".


On the first reading, I didn't pick up on a space. It's better to put sed code in code blocks.

echo 'inp1.inTicks_vl(ch_inTicks_vl);' | sed 's/^[^ ]*\([[:digit:]]\)\.inTicks_vl[^ ]*/inTicks_v1\1/'

The ^ outside square brackets matches the beginning of a line.

carolflb 02-05-2010 08:02 AM

Hi jschiwal,

thanks a lot for the explanation. It helped a lot. I just wanted to ask one more thing:

is [[:digit:]] the same thing as [0-9]? Which is the difference?

pixellany 02-05-2010 08:16 AM

They are the same.

There is a complete list of the definitions here:

carolflb 02-05-2010 08:22 AM

Thanks for the link pixellany :)

jschiwal 02-06-2010 01:20 AM

There is also a "regex (7)" manpage as well.

All times are GMT -5. The time now is 12:08 PM.