Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sed - replacing text with colon

Tags:

sed

I have this command:

sed -i  "s/a:b/b:c/g" file.txt

(in English: replace "a:b" with "b:c" in file.txt)

This doesn't work because of the colons in the subsitution text. How should I re-write the command?

like image 794
user1508893 Avatar asked Jan 17 '13 22:01

user1508893


People also ask

What does sed S do?

The s command is probably the most important in sed and has a lot of different options. Its basic concept is simple: the s command attempts to match the pattern space against the supplied regexp; if the match is successful, then that portion of the pattern space which was matched is replaced with replacement.

How do you use slash in sed?

When in doubt, echo the command: echo sed "s/\//\\\//g" -> sed s/\//\\//g . Btw you can use something else for sed, like 's@/@\\/@g' .


2 Answers

In case you want be safe , you can escape the : colon

sed -re "s/a\:b/b\:c/g" temp.txt

like image 197
Mirage Avatar answered Nov 02 '22 23:11

Mirage


This worked for me:

-->cat 1
a:bLINE1
a:bLINE2

-->cat 1 | sed 's/a:b/b:c/g'
b:cLINE1
b:cLINE2
like image 41
A B Avatar answered Nov 03 '22 00:11

A B