Wednesday, February 26, 2014

Replacing text in all files in unix

##########################################################

for f_name in `ls file*`
do

 more $f_name | sed "s/$1/$2/g" > f_name_temp
 mv f_name_temp $f_name

done

##########################################################

Experiment:

IOB_ksh >more file1 file2 file3
::::::::::::::
file1
::::::::::::::
this is file1
::::::::::::::
file2
::::::::::::::
this is file2
::::::::::::::
file3
::::::::::::::
this is file3

IOB_ksh >changeallfiles.sh this Dis

IOB_ksh >more file1 file2 file3
::::::::::::::
file1
::::::::::::::
Dis is file1
::::::::::::::
file2
::::::::::::::
Dis is file2
::::::::::::::
file3
::::::::::::::
Dis is file3

Comments:
The for loop condition should be changed accordingly. This works only in current directory files.