Search and replace within files on Solaris 8

Hi Experts,

I need to perform a search and replace of text on Solaris 8 with the following conditions:
1. Performed on TEXT files (NOT based on the file extension).
2. The text files must be automatically identified.
3. To be recursively performed on all files within a directory.
4. Output a report on exactly were the text was replaced.
5. Interactive S&R would be a bonus, but not mandatory.

Please direct me to a freeware application (preferably GUI app), or send me a script to do this.

Many thanks!

Regards,
Tal.
travehAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

wesly_chenCommented:
Hi,
---------
#1/bin/sh

for files in `find <path to the directory> -type f -exec file {} \; | grep  ASCII | awk -F: '{print $1}'`
do
   grep <search string> $file
   if $?
   then
      exit
   else
      echo $file > /tmp/file_replaced        
      sed 's/<search string>/<replace string>/g' $files > /tmp/`basename $files`
      /bin/mv /tmp/`basename $files`files
   fi
done
------

Regards,

Wesly
0
ahoffmannCommented:
perl -i.bak -pe 's/lamb/sheep/g' `find directory -type f -exec file {} \; |egrep -i '(program|ascii|english) text|:.text$|script$'|awk -F: '{print $1}'`
0
yuzhCommented:
Here's a vi version:

(find directory -type f -exec file {} \; | egrep -i 'text$|script$' |awk -F: '{print $1}' |  xargs vi -e -c "%s/OldString/Newstring/g" -c "wq" ) 2>/dev/null
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

ahoffmannCommented:
> vi -e   ??
you mean vim!
0
travehAuthor Commented:
Arff.

None of these worked... output of the first one was:

forest:/home2/WebSphere [78]> cat ~/search_and_replace.sh
#1/bin/sh

for files in `find <path to the directory> -type f -exec file {} \; | grep  ASCII | awk -F: '{print $1}'`
do
   grep <search string> $file
   if $?
   then
      exit
   else
      echo $file > /tmp/file_replaced        
      sed 's/amazone/forest/g' $files > /tmp/`basename $files`
      /bin/mv /tmp/`basename $files`files
   fi
done

forest:/home2/WebSphere [79]> ~/search_and_replace.sh
Ambiguous output redirect.
for: Command not found.
do: Command not found.
search: No such file or directory.
if: Empty if.


0
travehAuthor Commented:
Pardon me... I'm a moron... forgot to replace some stuff there...
0
wesly_chenCommented:
Hi,

Please change my script
-----------------
#1/bin/sh

for files in `find <path to the directory> -type f -exec file {} \; | grep -i text | awk -F: '{print $1}'`
do
   grep <search string> $files
   if $?
   then
      exit
   else
      echo $files > /tmp/file_replaced        
      sed 's/<search string>/<replace string>/g' $files > /tmp/`basename $files`
      /bin/mv /tmp/`basename $files` $files
   fi
done
----------------
Sorry for my typos ($file -> $files).

Regards,

Wesly
0
travehAuthor Commented:
Ehm... it still doesn't work...:

forest:/home2/WebSphere [88]> cat ~/search_and_replace.sh
#1/bin/sh

for files in `find . -type f -exec file {} \; | grep  ASCII | awk -F: '{print $1}'`
do
   grep amazone $file
   if $?
   then
      exit
   else
      echo $file > /tmp/file_replaced        
      sed 's/amazone/forest/g' $files > /tmp/`basename $files`
      /bin/mv /tmp/`basename $files`files
   fi
done

forest:/home2/WebSphere [85]> ~/search_and_replace.sh
^Z
[1]  + Suspended                     ~/search_and_replace.sh
forest:/home2/WebSphere [86]> bg
[1]    ~/search_and_replace.sh &
forest:/home2/WebSphere [88]> for: Command not found.
do: Command not found.
file: Undefined variable.

[1]    Exit 1                        ~/search_and_replace.sh

I guess I'll try again the perl / vi suggestions...

Tal.
0
travehAuthor Commented:
Wesly,

I saw your last comment after putting in mine...
I'll try again with the changes you suggested... (tomorrow - I'm going home now, it's 10am...)

Thanks!
Tal.
0
wesly_chenCommented:
Hi,

  To meet your fifth requirement, I modify my script as follow:
--------------
#1/bin/sh

script_name=`basename $0`
if [ $# -ne 2 ]
then
    echo "Syntax Error!"
    echo "Usage:    $script_name <search string> <replace string>"
    exit 1
fi

search_string=$1     # read first parameter
replace_string=$2    # read second parameter

rm -f /tmp/file_replaced   # clean the previous report file

for files in `find <path to the directory> -type f -exec file {} \; | grep -i text | awk -F: '{print $1}'`
do
   grep $search_string $files > /dev/null 2>&1
   if [ "$?" = "0" ]
   then
      echo $files >> /tmp/file_replaced        # report which file been replace
      sed "s/$search_string/$replace_string/g" $files > /tmp/`basename $files`
      /bin/mv /tmp/`basename $files` $files
   else
      continue
   fi
done
------------------------

Wesly
0
ahoffmannCommented:
> #1/bin/sh
should be
   #!/bin/sh
0
wesly_chenCommented:
Thank you. My fat fingers are getting worse. ^_^

Wesly
0
yuzhCommented:
Sorry, comment http:12692863 is a vim version, if you have vim installed, it should looks
like:

(find directory -type f -exec file {} \; | egrep -i 'text$|script$' |awk -F: '{print $1}' |  xargs vim -e -c "%s/OldString/Newstring/g" -c "wq" ) 2>/dev/null

If you want to make it 2 lines, it should be:

(find directory -type f -exec file {} \; | egrep -i 'text$|script$' |awk -F: '{print $1}' | \
xargs vim -e -c "%s/OldString/Newstring/g" -c "wq" ) 2>/dev/null

you can get vim from:
http://sunfreeware.com/

"Perl" would  run fast than "sed", sed is available for all Solaris releases.  Solaris 8 or newer ship with perl.
0
wesly_chenCommented:
For the perl script ahoffman provide as follow:
perl -i.bak -pe 's/lamb/sheep/g'
It will create *.bak, which is annoying.
In addition to removing it manually, any option for perl to not leave *.bak files?

Wesly
0
yuzhCommented:
To use "perl" without tmp file you can do:

perl -i -pe "s/lamb/sheep/" filename

You can make it looks like:

find directory -type f -exec file {} \; | egrep -i 'text$|script$' |awk -F: '{print $1}' | xargs perl -i -pe "s/lamb/sheep/"


0
wesly_chenCommented:
As recommended:
$ search_and_replace.sh amazone forest
-search_and_replace.sh ------
#!/bin/sh

script_name=`basename $0`
if [ $# -ne 2 ]
then
    echo "Syntax Error!"
    echo "Usage:    $script_name <search string> <replace string>"
    exit 1
fi

search_string=$1     # read first parameter
replace_string=$2    # read second parameter

rm -f /tmp/file_replaced   # clean the previous report file

for files in `find <path to the directory> -type f -exec file {} \; | grep -i text | awk -F: '{print $1}'`
do
   grep $search_string $files > /dev/null 2>&1
   if [ "$?" = "0" ]
   then
      echo $files >> /tmp/file_replaced        # report which file been replaced
      perl -i -pe "s/$search_string/$replace_string/g" $files   <=== use perl for faster result
   else
      continue
   fi
done
----

Wesly
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
travehAuthor Commented:
Thanks!

The last one worked very nicely!

I decided to distribute the points, though, since yuzh and ahoffmann made some contribution too...

I have another question now (continueing this one), but I will post a new one.

Many thanks!

Tal.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Unix OS

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.