Replace all occurrences of a string

Discussion in 'Perl' started by Shabam, Nov 16, 2004.

  1. Shabam

    Shabam Guest

    Is there a utility or command that will allow me to replace all occurrences
    of a string within all files in a directory?

    I'm trying to mass update IP addresses of all DNS zone files in my
    /var/named/ folder.
     
    Shabam, Nov 16, 2004
    #1
    1. Advertising

  2. Shabam

    Jim Gibson Guest

    In article <>, Shabam
    <> wrote:

    > Is there a utility or command that will allow me to replace all occurrences
    > of a string within all files in a directory?
    >
    > I'm trying to mass update IP addresses of all DNS zone files in my
    > /var/named/ folder.


    You can use a Perl one-liner of the type

    perl -pi'bak' -e 's/bar/baz' /var/named/*

    See 'perldoc perlrun' for more options. Try it out on some test files
    first!
     
    Jim Gibson, Nov 17, 2004
    #2
    1. Advertising

  3. Shabam

    Joe Smith Guest

    Jim Gibson wrote:
    > In article <>, Shabam
    > <> wrote:
    >
    >
    >>Is there a utility or command that will allow me to replace all occurrences
    >>of a string within all files in a directory?
    >>
    >>I'm trying to mass update IP addresses of all DNS zone files in my
    >>/var/named/ folder.

    >
    >
    > You can use a Perl one-liner of the type
    >
    > perl -pi'bak' -e 's/bar/baz' /var/named/*
    >
    > See 'perldoc perlrun' for more options. Try it out on some test files
    > first!


    You'll need the closing slash and probably the g modifier on that s///.

    perl -pi.bak -e 's/oldtext/newtext/g' /var/named/*

    More sophisticated stuff with /e.

    perl -pi -e 's/(10\.0\.)(\d+)(\.\d+)/$1.($2+100).$3/e' /var/named/*
     
    Joe Smith, Nov 17, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    796
    Malcolm
    Oct 2, 2005
  2. bahoo
    Replies:
    37
    Views:
    863
    Paul McGuire
    Apr 9, 2007
  3. herman
    Replies:
    5
    Views:
    7,673
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=
    Aug 30, 2007
  4. yogi_bear_79
    Replies:
    9
    Views:
    5,052
    James Kanze
    Feb 28, 2008
  5. Replies:
    4
    Views:
    141
    Tim Hunter
    Feb 10, 2009
Loading...

Share This Page