Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search and Replace.

Status
Not open for further replies.

teky

Programmer
Mar 24, 2000
55
US
Hi all,

I have about 150 html files in my unix system and my
company decided to change a word from x to y.
I need to replace the word in all the html files
in the sub directories too.
Replacing the word manually is a time consuming process.
Can anybody provide me with a script that replaces a word
from x to y in all the files.

Thanks,
Teky.


 
Hi,
Do the following:
go to the dir where all files are there.
open a file say test using vi editor.
give the following command (Esc :r! ls) --to list all files
then give (Esc :1,$s/^/vi )--to insert vi in front,note there's a space after vi
then give (Esc :1,$s/$/<change)--to append <change in end
save the file and quit.
open another file change using vi and type the following in insert mode:)1,$s/x/y) in first line and :)wq!) in second line.save this file and quit.
this is to replace x by y.
then execute the file test and yo all your files will replace the character x by y.


I hope u find this helpful......
 
Thanks Navin,

Why do we need the second file.
Anyway, I will try to execute this script.

Teky.


 
Yes is a good one, you can also do all with a very simple shell script, on the example one to change &quot;teste&quot; to &quot;test&quot; on all files with extension html on directory &quot;/usr/local/myfiles&quot; and all subdirectories just do:

# sere.sh -ext html -dir /usr/local/myfiles teste test

Hope this helps,

Regards,

Carlos Almeida,

#sere.sh
#! /bin/sh
usage() {
cat<<EOF
usage: $0 [-ext extension] [-dir directory_base] from_keyword to_keyword
EOF
exit 1
}

BASEDIR=&quot;.&quot;
EXT=&quot;&quot;

while : ; do
case $1 in
-dir) test $# -gt 1 || usage; BASEDIR=&quot;$2&quot; ; shift ;;
-ext) test $# -gt 1 || usage; EXT=&quot;$2&quot; ; shift ;;
-*) usage ;;
*)
break ;;
esac
shift
done

test $# -eq 2 || usage

if test -d &quot;$BASEDIR&quot; ; then : ; else
echo &quot;$BASEDIR: no such directory&quot;
exit 1
fi

case $EXT in &quot;&quot;) ;; .*) ;; *) EXT=&quot;.$EXT&quot; ;; esac
for file in `find &quot;$BASEDIR&quot; -type f -name &quot;*$EXT&quot; -print`; do
ed <<EOF $file >/dev/null 2>&1
1,\$s/$1/$2/g
w
q
EOF
done
 
If you have perl installed, you can use a really handy one-liner:

perl -pi -e 's/oldword/newword/gi' *.html

The switches at the command line (p,i,e) just say loop (p), edit the file in-place (i), and execute (e) the command (in single quotes, the gi = global, case-insensitive) on the filename = *.html.

If you want to make backup copies of the files before replacing them, use:

perl -p -e 's/old/new/gi' -i.bak *.html

It isn't recursive, but it should make it a bit easier.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top