Reputation: 3
I am new to sed
. I want to replace a substring
for example:
var1=server1:game1,server2:game2,sever3:game1
output should be
server1 server2 server3 (with just spaces)
I have tried this.
echo $var1 | sed 's/,/ /g' | sed 's/:* / /g'
This is not working. Please suggest a solution.
Upvotes: 0
Views: 229
Reputation: 20131
Just for info, you are really only matching, not replacing, so grep can be your friend (with -P):
grep -oP '[^:,=]+(?=:)'
That matches a number of characters that aren't :,=
followed by a :
using lookahead.
This will put the servers on different lines, which may be what you want anyway. You can put them on one line by adding tr
:
grep -oP '[^:,=]+(?=:)' | tr '\n' ' '
Upvotes: 0
Reputation: 41446
An awk
variation using same regex
as sed
awk '{gsub(/:[^,]+,?/," ")}1' <<< "$var1"
PS Its always good custom to "quote" variables
Upvotes: 0
Reputation: 14949
You can try this sed
,
echo $var1 | sed 's/:[^,]\+,\?/ /g'
Explanation:
:[^,]\+,
- It will match the string from :
to ,
\?
- Previous may occur or may not ( Since end of line don't have ,
)
Upvotes: 1
Reputation: 415
echo $var1 | sed s/:game[0-9],*/\ /
Assuming your sub string has game
followed by a number([0-9]*
)
Upvotes: 0