Shell sed to replace many words in many files

Hi everyone.

I try to replace lot of network mask in many files and after hours I am blocked, I am stupid with sed. This is my goal:

I would like to replace every
Code:
255.255.255.255 by 32
255.255.255.254 by 31
255.255.255.252 by 30
255.255.255.248 by 29
255.255.255.240 by 28
etc..
and this to every netmask ( 32 to 1) in every file (text files with .ip4 extension) inside a folder. I try different logic like:
Code:
#!/bin/sh
echo enter file name with extension
read NAME
echo enter word to replace
read FIND
echo enter word that replaces
read REPLACE
for file in $(/usr/bin/grep -il "$FIND" $NAME)
do
sed -e "s/$FIND/$REPLACE/ig" $file > /tmp/tempfile.tmp
mv /tmp/tempfile.tmp $file
done
but in my case I don't want to specify the name of the file. I would like to do it in every file inside a folder and the most important: how can I replace 32 times some "words" in the same file and do it recursively for every file (text files only) in the folder. I'm blocked to use the tempfile in a "clever" way.

I am not a developer and I know my logic is bad to do this task. If somebody can help me to understand the good way to do this he will make me very happy. It is to use it with my FreeBSD router as file tables.

Thank you so much for your time.
Clem
 
I recommend a review of the sed(1) manpage.

I believe the command line option you need for in-place edits is sed -i ".bak". If you don't want to store backups of the files prior to editing, use option sed -i "".
 
As for searching all files in a folder, instead of specifying a file name, specify a folder name and then follow it with a $FOLDER/*
 
Sorry, I don't have time to fully answer this but this will get you started:

You could run a command like this for each of the substitutions (32 of them):
Code:
find <FOLDER_NAME_HERE> -name "*.ip4" | xargs sed -i -r 's/.*.255$/32/g'
find <FOLDER_NAME_HERE> -name "*.ip4" | xargs sed -i -r 's/.*.254$/31/g'
find <FOLDER_NAME_HERE> -name "*.ip4" | xargs sed -i -r 's/.*.252$/30/g'

And by using awk you could maybe shorten the whole thing into a one line shell command but I can't think of how off the top of my head.
 
Thank you so much segfault I do the script now with your precious advices...
If you come in Paris one day, contact me.
We take some beers together ;-)
 
Haha thanks Clementbsd, my wife and I actually hope to visit France for our next trip, but neither of us drink. But perhaps you could give me the name of your favourite resturant so we could treat our Canadian taste buds with some french cuisine!
;)
 
Top