Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to add a "/" before each number using sed:

    echo 1 2 3 4 5 6 7  | sed 's/[ ^]*/&\//g'

    /1 /2 /3 /4 /5 /6 /7/

When I use this syntax, why do I get the "/" after 7? How can I fix my sed syntax in order to get this:

    /1 /2 /3 /4 /5 /6 /7
share|improve this question
up vote 2 down vote accepted

Give this a try:

echo '1 2 3 4 5 6 7' | sed 's|\w\+|/&|g'
share|improve this answer
    
An explanation would be nice. – mmdemirbas Oct 11 '12 at 8:57

to fix it: prepend the slash before the beginning of a number (i use the :to separate pattern and replacement instead of /, no need to escape the slash then)

 % echo 1 2 3 4 5 6 7 | sed 's:\([1-9]\+\):/\1:g'

the problem of your command is, that you use the *to match either space or a carret ^. and not just one of them but any number of them, including 0. that matches at the end of the line (after the 7) as well (and luckily for you also at the beginning).

to make this work with either word (aka nonspace) use this:

 % echo ha hu 1 2 ho | sed 's:[^ ]\+:/&:g'
share|improve this answer
    
I get /1 /2 /3 /4 /5 /6 /71 (why 1 number after 7?) – yael Jan 23 '11 at 14:00
    
you get 1 because i added a 1 to the last 7 to give you a pattern that works on any integer, not only single digits. – akira Jan 23 '11 at 14:03
    
but I dont want 1 after 7 – yael Jan 23 '11 at 14:08
    
any way this is numbers example could be words and your sed fit for numbers – yael Jan 23 '11 at 14:10
    
then a) be more specific in your question and b) replace "71" with "7" by pure imagination. – akira Jan 23 '11 at 14:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .