Link to home
Start Free TrialLog in
Avatar of traveh
traveh

asked on

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.
Avatar of wesly_chen
wesly_chen
Flag of United States of America image

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
SOLUTION
Avatar of ahoffmann
ahoffmann
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of yuzh
yuzh

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
> vi -e   ??
you mean vim!
Avatar of traveh

ASKER

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.


Avatar of traveh

ASKER

Pardon me... I'm a moron... forgot to replace some stuff there...
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
Avatar of traveh

ASKER

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.
Avatar of traveh

ASKER

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.
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
> #1/bin/sh
should be
   #!/bin/sh
Thank you. My fat fingers are getting worse. ^_^

Wesly
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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/"


ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of traveh

ASKER

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.