LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Trouble using sed (https://www.linuxquestions.org/questions/programming-9/trouble-using-sed-4175429670/)

Xplorer4x4 09-29-2012 07:35 PM

Trouble using sed
 
I need to replace 127.0.0.1 on line 49 with an *

I tried:
Code:

sed '49s/127.0.0.1/*/'
No luck. I tried some variations using double quotes and such with no luck.

amboxer21 09-29-2012 07:42 PM

Try this

Code:

sed -e '49s/127.0.0.1/*/g' filename.txt

billyoc 09-29-2012 08:05 PM

Quote:

Originally Posted by amboxer21 (Post 4792875)
Code:

sed -e '49s/127.0.0.1/*/g' filename.txt
I'm using a bash shell btw.

I can't reproduce the problem, what you have works here.

GNU bash, version 4.2.10(1)-release (i686-pc-linux-gnu)
GNU sed version 4.2.1

Xplorer4x4 09-29-2012 08:14 PM

Quote:

Originally Posted by amboxer21 (Post 4792875)
Code:

sed -e '49s/127.0.0.1/*/g' filename.txt
I'm using a bash shell btw.

I am using a bash shell on a ubuntu server. I tested this and I see it print the entire script and it shows me the change but when I review the file manually in nano, 127.0.0.1 is still in the file. For what it is worth, the entire line is
Code:

"rpc-whitelist": "127.0.0.1",

amboxer21 09-29-2012 08:29 PM

Quote:

Originally Posted by Xplorer4x4 (Post 4792899)
I am using a bash shell on a ubuntu server. I tested this and I see it print the entire script and it shows me the change but when I review the file manually in nano, 127.0.0.1 is still in the file. For what it is worth, the entire line is
Code:

"rpc-whitelist": "127.0.0.1",

It's because your not writing the changes to file. It wouldn't make a difference though. Sed is searching for the string 127.0.0.1 and replacing it with an asterisk. So the text before it doesn't matter.

amboxer21 09-29-2012 08:32 PM

Use the -i switch if you want to apply changes to the same file

Code:

sed -i '49s/127.0.0.1/*/g' filename.txt

Xplorer4x4 09-29-2012 08:35 PM

As I said in the OP, I needed to replace a lin so I tried the -i switch and it worked great! Thanks for the quick response!

David the H. 09-30-2012 11:19 AM

It's cleaner to use the [c]hange command instead of the [s]ubstitution command here.

Code:

sed -i '49c*' file.txt
Here are a few useful sed references:
http://www.grymoire.com/Unix/Sed.html
http://sed.sourceforge.net/grabbag/
http://sed.sourceforge.net/sedfaq.html
http://sed.sourceforge.net/sed1line.txt


Another option that I like to use is ed. As a true text editor it's more flexible in many circumstances. But it can also be a bit more hassle to set up.

Code:

ed -s file.txt <<<$'49c\n*\n.\nw'
How to use ed:
http://wiki.bash-hackers.org/howto/edit-ed
http://snap.nlc.dcccd.edu/learn/nlc/ed.html
(also read the info page)


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