1

I have a string as follows:

/test/test/ 12 /test/test

My goal is to turn it into:

/test/est/ 12 /test/test

This is the command:

echo "$x" | sed 's@\(/*/\).@\1@g'"

I am using at the moment but it is applying it everywhere instead of at the second instance of "/"

3
  • could you please update your post and show us what is your string and what you want as a result ? Its unclear.
    – Rahul
    Commented Apr 27, 2016 at 12:18
  • g means global - everywhere
    – Jeff Schaller
    Commented Apr 27, 2016 at 12:22
  • If you have GNU sed, you can replace just the nth match explicitly e.g. sed 's@/.@/@2' Commented Apr 27, 2016 at 12:24

3 Answers 3

1

As stated by steeldriver, if you use GNU sed, you can tell which occurrence should be replaced, e.g.:

echo "/test/test/ 12 /test/test" | sed -n -e  's_/._/_2 p' 

If you can not make use of this feature you can also write:

echo "/test/test/ 12 /test/test" | sed -n -r -e  's_(/[a-z]([a-z]+))\1_\1/\2_ p'

Biliography: http://www.grymoire.com/Unix/Sed.html [Using \1 to keep part of the pattern]

0
echo "/test/test/ 12 /test/test" | sed 's/\/test\/t/\/test\//'
0

Nice and easy...

sed -n "s/test/est/2p"
  • -n don't print anything
  • s/ substitute
  • "test" with "est"
  • /2 but only the second instance
  • p print the results
1
  • Or even sed "s/test/est/2"
    – user167612
    Commented May 27 at 14:42

You must log in to answer this question.

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