Re: replacing a line in a file

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]



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 -----------------------------

Attachment: pgpLT14PTxkPZ.pgp
Description: PGP signature

_______________________________________________
CentOS mailing list
CentOS@xxxxxxxxxx
http://lists.centos.org/mailman/listinfo/centos

[Index of Archives]     [CentOS]     [CentOS Announce]     [CentOS Development]     [CentOS ARM Devel]     [CentOS Docs]     [CentOS Virtualization]     [Carrier Grade Linux]     [Linux Media]     [Asterisk]     [DCCP]     [Netdev]     [Xorg]     [Linux USB]
  Powered by Linux