LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   SED - substitution (https://www.linuxquestions.org/questions/linux-newbie-8/sed-substitution-787211/)

carolflb 02-05-2010 06:02 AM

SED - substitution
 
Hi all,

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

inp1.inTicks_vl(ch_inTicks_vl);

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:

inTicks_vl1

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:

inTicks_vl


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 06: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.
Code:

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

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

carolflb 02-05-2010 07: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 07:16 AM

They are the same.

There is a complete list of the definitions here:
http://tldp.org/LDP/abs/html/x16608.html

carolflb 02-05-2010 07:22 AM

Thanks for the link pixellany :)

jschiwal 02-06-2010 12:20 AM

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


All times are GMT -5. The time now is 02:01 PM.