[K12OSN] How to change X-terminales networks
Nils Breunese
nils at breun.nl
Wed Oct 22 12:04:06 UTC 2008
Rob Owens wrote:
> I'm not an expert on the syntax, but I know you can use 'sed' for
> this.
> It'll find and replace text in any file. Something like:
>
> sed s/192.168.0.254/192.168.10.1/g myconfigfile.txt >
> myconfigfile.txt.2
>
> That'll find the first IP address in myconfigfile.txt, and replace it
> with the 2nd IP address in myconfigfile.txt.2 -- you'll then have to
> delete the original config file and replace it with the new one.
>
> You could probably put together a script that does a recursive grep
> (grep will find text in a file) and the run sed on that file. This is
> probably wrong, but it might go something like this:
>
> #!/bin/bash
>
> # Change these variables to suit your needs
> OLDIP=192.168.0
> NEWIP=192.168.10
> SEARCHDIR=/etc
>
> for FILE in `ls -R $SEARCHDIR`
> do
> grep $OLDIP $FILE
> if [ $? == 0 ]; then
> sed s/$OLDIP/$NEWIP/g $FILE > $FILE.new && mv -f $FILE.new $FILE
> fi
> done
Instead of creating a new file using sed and then overwriting the old
file you can use sed with the in-place (-i) flag. If you specify an
extension with the -i flag, then a backup of the original file will be
saved using that extension.
You can also replace strings inline using Perl for instance:
perl -p -i -e 's/OLD/NEW/g' files
Nils Breunese.
More information about the K12OSN
mailing list