Sunday, April 29, 2007

Replace .htm with .html in 100's of files inside a dir

I needed to replace {.htm} files with {.html} extensions in
100's of files in a directory :

Heres what I did :

[root@unixguy scripts]# ls
file1.htm file2.htm file3.htm

[root@unixguy scripts]# for list in `ls -1t *.htm*`
> do
> prefix=`echo $list | awk -F"\." '{print $1}'`
> mv $list ${prefix}.html
> done


[root@unixguy scripts]# ls
file1.html file2.html file3.html


One more way of doing this :

[root@unixguy scripts]# ls
file1.html file2.html file3.html

[root@unixguy scripts]# ls *.html | awk -F '.'
'{print "mv "$1".html "$1".htm"}'| csh

[root@unixguy scripts]# ls
file1.htm file2.htm file3.htm

Search and replace Shell Script

Had to parse 100's of files in a loop and replace a word
called Bombay with a new word Mumbai

cat file1.dat
city name is Bombay and country is INDIA.

To:
cat file1.dat
city name is Mumbai and country is INDIA.

for file in /tmp/a /tmp/c /tmp/b ; do
sed 's/Bombay/MUMBAI/g' ${file} > ${file}.new
done