[CentOS] replacing a line in a file

fred smith fredex at fcshome.stoneham.ma.us
Sun Jan 25 03:21:37 UTC 2009


On Sat, Jan 24, 2009 at 10:01:56PM -0500, Jerry Geis wrote:
> I have a large file that has a line like:
> 
> bindaddr=0.0.0.0     ; some other text
> 
> I want to replace the 0.0.0.0 with my address 192.168.1.8 and remove 
> everything
> else on the line to get:
> 
> bindaddr=192.168.1.8
> 
> How can I do that?

given the power of unix/linux, there's probably a bazillion ways.
If you want to do it from the commandline without having to use an
interactive text editor, you could do it this way (note this is UNTESTED):

	sed -e "s/^bindaddr=0\.0\.0\.0.*$/bindaddr=192\.168\.1\.8/" filename

Note the backslashes to turn the dots into literal dots, not the
regexp character that it would be without the backslash. also note
the single instance of a dot without a leading backslash, because
that is intended to be a regexp.

OK, now it's tested. given this input file:

bindaddr=1.0.0.0     ; some other text
bindaddr=2.0.0.0     ; some other text
bindaddr=0.0.0.0     ; some other text
bindaddr=3.0.0.0     ; some other text

applying the command above produces:

bindaddr=1.0.0.0     ; some other text
bindaddr=2.0.0.0     ; some other text
bindaddr=192.168.1.8
bindaddr=3.0.0.0     ; some other text

Other people would come up with something in AWK, or Perl or Ruby
or PHP or <insert language du jour here>. I suppose someone with time on
his hands might even figure out how to do it purely in BASH, without
resorting to any external programs.


May God rest your soul if you conned me into doing your homework for you! :)


-- 
-------------------------------------------------------------------------------
    Under no circumstances will I ever purchase anything offered to me as
    the result of an unsolicited e-mail message. Nor will I forward chain
    letters, petitions, mass mailings, or virus warnings to large numbers
    of others. This is my contribution to the survival of the online
    community.
 --Roger Ebert, December, 1996
----------------------------- The Boulder Pledge -----------------------------
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
URL: <http://lists.centos.org/pipermail/centos/attachments/20090124/40328857/attachment.sig>


More information about the CentOS mailing list