Operating System - HP-UX
1833758 Members
2694 Online
110063 Solutions
New Discussion

Re: replace text string and rewrite file script

 
SOLVED
Go to solution
Bill McNAMARA_1
Honored Contributor

replace text string and rewrite file script

Hi all,
I'm looking for a script that'll recursively search files in a directory structure and replace all occurances of a string to the current hostname.

for example I've got around 30 files with a text string enter_hostname and I want a script that when I run the script with for example
./script -old enter_hostname -new hostname -recurse thisdir

I use find . * |xargs grep enter_hostname to search for the hostname but it finds strings in binary files that I need to avoid rewriting.
I just want to rewrite text files.

any ideas/suggestions?

Later,
Bill
It works for me (tm)
8 REPLIES 8
Stefan Farrelly
Honored Contributor

Re: replace text string and rewrite file script


do a file * in the directory file and grep those that are described as ascii text only.
Im from Palmerston North, New Zealand, but somehow ended up in London...
James R. Ferguson
Acclaimed Contributor

Re: replace text string and rewrite file script

Hi Bill:

A quick suggestion is to use 'file' to filter out non-ascii candidates from your process stream. I know how much you hate 'awk', so:

For example:

# file /etc/hosts|awk '{print $2}'

...returns ascii

# file /usr/bin/vi|awk '{print $2}'

...returns s800

Regards!

...JRF...
Sachin Patel
Honored Contributor

Re: replace text string and rewrite file script

Hi Bill,
you can use command "/usr/bin/file" to check for ascii or binary.

Sachin
Is photography a hobby or another way to spend $
Bill McNAMARA_1
Honored Contributor

Re: replace text string and rewrite file script

okay I got that much, but what I want to do is get the script together that'll rewrite the file with the new hostname.

I've got to do some sort of file io script that
seds the file perhaps and replaces all occurances of string with newstring... and then rewrites the file.

At the moment, I list all files, then vi them all and use :1:$/s/oldstring/newstring/g
in vi.
I want a script to do it...

Later,
Bill
It works for me (tm)
Stefan Farrelly
Honored Contributor

Re: replace text string and rewrite file script


Once youve identified the ascii text filenames then simply do;

cat | sed 's///g' >
Im from Palmerston North, New Zealand, but somehow ended up in London...
Thom Cornwell
Frequent Advisor

Re: replace text string and rewrite file script

$ for f in $(file *|grep ascii|cut -d":" -f1)
> do
> ex ${f} > done

where exscript contains your vi command
Robin Wakefield
Honored Contributor
Solution

Re: replace text string and rewrite file script

find . -type f | xargs file | grep ascii | cut -d: -f 1 | while read file ; do
sed "s/oldname/newname/g" $file > $file.new
mv $file.new $file
done

would do it.

Robin.
James R. Ferguson
Acclaimed Contributor

Re: replace text string and rewrite file script

Hi Bill:

#!/usr/bin/sh
#
typeset OLD=$1
typeset NEW=$2
#
for F in *
do
if [ `file $F|awk '{print $2}'` = ascii ]
then
sed "s/$OLD/$NEW/g" $F > ${F}.new
mv $F.new $F
fi
done
#.end.

...JRF...