Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sed - substitute either of two characters with one command

Tags:

sed

I would like one sed command to accomplish the following:

$ sed s'/:/ /g' <and> sed s'/=/ /g'

That is, I would like to write

sed s'/<something>/ /g' 

and have both = and : replaced by space.

like image 297
lidia Avatar asked Jul 26 '10 12:07

lidia


People also ask

Does sed support multiline replacement?

By using N and D commands, sed can apply regular expressions on multiple lines (that is, multiple lines are stored in the pattern space, and the regular expression works on it): $ cat two-cities-dup2.

How do you use sed for substitution?

Find and replace text within a file using sed command Use Stream EDitor (sed) as follows: sed -i 's/old-text/new-text/g' input.txt. The s is the substitute command of sed for find and replace. It tells sed to find all occurrences of 'old-text' and replace with 'new-text' in a file named input.txt.

What does \b mean in sed?

\b marks a word boundary, either beginning or end. Now consider \b' . This matches a word boundary followed by a ' . Since ' is not a word character, this means that the end of word must precede the ' to match.


2 Answers

sed s'/[:=]/ /g'

Brackets mean "any one of".

like image 108
Sanjay Manohar Avatar answered Oct 20 '22 01:10

Sanjay Manohar


One option is also to use sed -e, like this. Although you don't need it in this case, it's however a good option to know about.

sed -e 's/:/ /' -e 's/..../ /' file
like image 39
Anders Avatar answered Oct 20 '22 01:10

Anders