[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2778
  • Last Modified:

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.
0
traveh
Asked:
traveh
  • 6
  • 5
  • 3
  • +1
3 Solutions
 
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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
 
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 6
  • 5
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now