Replace all occurrences of a string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Shabam

    Replace all occurrences of a string

    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.


  • Jim Gibson

    #2
    Re: Replace all occurrences of a string

    In article <aOadnR3r7tGRHQ fcRVn-ug@adelphia.com >, Shabam
    <xxredbaronxxx@ hotmail.com> wrote:
    [color=blue]
    > 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.[/color]

    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!

    Comment

    • Joe Smith

      #3
      Re: Replace all occurrences of a string

      Jim Gibson wrote:[color=blue]
      > In article <aOadnR3r7tGRHQ fcRVn-ug@adelphia.com >, Shabam
      > <xxredbaronxxx@ hotmail.com> wrote:
      >
      >[color=green]
      >>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.[/color]
      >
      >
      > 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![/color]

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

      Comment

      Working...