LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   replacing a line with spaces (https://www.linuxquestions.org/questions/linux-newbie-8/replacing-a-line-with-spaces-854289/)

balakodoth 01-05-2011 12:14 AM

replacing a line with spaces
 
I have a line like

port = 2566

I want to replace it as

port = 8080

does anyone knows how to do this in shell script. when i tried to do this with sed
sed -i 's/port=.*/port='$3'/' /root/$2
it is not recognizing spaces , it works only if line is port=2566 .

any idea on how to do this?

crts 01-05-2011 12:31 AM

Quote:

Originally Posted by balakodoth (Post 4213554)
I have a line like

port = 2566

I want to replace it as

port = 8080

does anyone knows how to do this in shell script. when i tried to do this with sed
sed -i 's/port=.*/port='$3'/' /root/$2
it is not recognizing spaces , it works only if line is port=2566 .

any idea on how to do this?

If your line has spaces then your regex needs spaces, too.
Code:

sed -i 's/port = .*/port = '$3'/' file

balakodoth 01-05-2011 12:46 AM

thanks... I tried giving space but it dosent work.. :-(

grail 01-05-2011 12:49 AM

Maybe it is a tab or a mix, try using character list instead:
Code:

sed -i "s/port[[:space:]]*=[[:space:]]*.*/port = $3/" file
The use of double quotes will help you use the variable you are passing in.

munavar 01-05-2011 01:04 AM

You can use this

sed -i 's/2566/8080/' FilePath

grail 01-05-2011 01:43 AM

@munavar - only if we assume that it exists nowhere else in the file

rhoekstra 01-05-2011 07:58 AM

Quote:

sed -i '/^port/s/2566/8080/' FilePath
using the '/^port/' in front of the 's/' statement only applies the change to lines that start with 'port'.


All times are GMT -5. The time now is 06:04 AM.