Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using sed to replace text with spaces with a defined variable with slashs [duplicate]

Tags:

bash

unix

sed

I am trying to use sed to replace a line with spaces with a defined variable.

For example,

I want to replace 'a dumb string' with $lan, and lan="~/afile.py

I assumed the line would be

sed "s/a dumb string/$lan/g" file.txt

but this leave the 'a dumb string' part blank in the file.txt.

My problem seems simple enough. Any help is much appreciated.

like image 849
NPB Avatar asked Mar 05 '13 23:03

NPB


People also ask

How do I change my sed delimiter?

An even less-known fact is that you can use a different delimiter even for patterns used in addresses, using a special syntax: # do this (ugly)... sed '/\/a\/b\/c\//{do something;}' # ...or these (better) sed '\#/a/b/c/#{do something;}' sed '\_/a/b/c/_{do something;}' sed '\%/a/b/c/%{do something;}' # etc.

How do you remove slash in sed?

Since the text we are dealing with contains / , we're using % as an alternative delimiter in the sed expressions. This first removes the string . git (possibly followed by a / ) from the end of each line of input before applying the same substitution as in the first sed command.


1 Answers

Sometime space in search string does not work so needed to use [[:space:]] in centos.

    sed -i 's/User[[:space:]]daemon/User apache/' httpd1.conf
like image 138
Vidya Patil Avatar answered Sep 16 '22 14:09

Vidya Patil