LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-29-2012, 07:35 PM   #1
Xplorer4x4
Member
 
Registered: Mar 2012
Location: Evansville,IN
Distribution: Manjaro KDE
Posts: 58

Rep: Reputation: Disabled
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.
 
Old 09-29-2012, 07:42 PM   #2
amboxer21
Member
 
Registered: Mar 2012
Location: New Jersey
Distribution: Gentoo
Posts: 291

Rep: Reputation: Disabled
Try this

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

Last edited by amboxer21; 09-29-2012 at 08:12 PM.
 
Old 09-29-2012, 08:05 PM   #3
billyoc
LQ Newbie
 
Registered: Oct 2010
Posts: 23

Rep: Reputation: 23
Quote:
Originally Posted by amboxer21 View Post
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
 
Old 09-29-2012, 08:14 PM   #4
Xplorer4x4
Member
 
Registered: Mar 2012
Location: Evansville,IN
Distribution: Manjaro KDE
Posts: 58

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by amboxer21 View Post
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",
 
Old 09-29-2012, 08:29 PM   #5
amboxer21
Member
 
Registered: Mar 2012
Location: New Jersey
Distribution: Gentoo
Posts: 291

Rep: Reputation: Disabled
Quote:
Originally Posted by Xplorer4x4 View Post
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.

Last edited by amboxer21; 09-29-2012 at 08:37 PM.
 
Old 09-29-2012, 08:32 PM   #6
amboxer21
Member
 
Registered: Mar 2012
Location: New Jersey
Distribution: Gentoo
Posts: 291

Rep: Reputation: Disabled
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
 
Old 09-29-2012, 08:35 PM   #7
Xplorer4x4
Member
 
Registered: Mar 2012
Location: Evansville,IN
Distribution: Manjaro KDE
Posts: 58

Original Poster
Rep: Reputation: Disabled
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!
 
Old 09-30-2012, 11:19 AM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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)
 
2 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Cant emerge SED and having other trouble after system update Adol Gentoo 2 05-31-2012 09:43 AM
[SOLVED] Trouble with sed. (Updated) lupusarcanus Linux - Newbie 12 12-24-2010 09:44 PM
trouble backreferencing with sed wds Programming 2 06-19-2008 08:13 PM
sed trouble hfawzy Programming 5 08-07-2006 10:37 PM
Having some trouble with sed P0ldy Linux - General 6 01-04-2006 04:26 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:54 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration