/* BULK FILE EDIT Author JP Vijaykumar Oracle DBA Date July 7th 2009 In one of our applications, a tool dumps few hundred files into a directory with extension *.dat everyday. Inside each file, the 3rd pattern "<>" in the lines that contain the word "jp_directory" is left blank. I need to come up with a shell script to go through each file with extension *.dat in my directory and fill the 3rd pattern "<>" in the line that contain "jp_directory", with the path "/opt/oracle/jp". This script does the required job: */ $ cat ravi.sh #!/bin/sh cd /opt/oracle/jp1 ls -1 *dat|while read FILE do cat $FILE|while read LINE do echo $LINE|sed '/jp_directory/s/<>/<\/opt\/oracle\/jp>/3'>>TEMP done mv TEMP /opt/oracle/jp1/$FILE.old done exit /* Here, instead of 3, if I use "g", then all the patterns "<>" will be filled with the path name. */ $ cat test1.dat this is a test doc and I want to run a shell script on the files in this directory jp_directory <> is <> to be <> to do some sort of script for Ravi and test on my test machine thanks and happy scripting $ ./ravi.sh /* For comparing the before and after files, in my script, I am renaming the modified with *old. */ $ cat test1.dat.old this is a test doc and I want to run a shell script on the files in this directory jp_directory <> is <> to be to do some sort of script for Ravi and test on my test machine thanks and happy scripting /* Now I want to complicate the script requirement. I want only the 3rd "<>" pattern after the word "jp_directory" ONLY to be filled with "/opt/oracle/jp" path. Not any other 3rd pattern is to be filled with the path "/opt/oracle/jp". */ $ cat bfe.sh #/bin/sh ls -1 jp*sql|while read FILE do echo $FILE cat $FILE|while read LINE do if [[ $(echo $LINE|grep -v grep|grep "jp_directory"|wc -l) > "0" ]]; then COUNT1=0 COUNT2=0 STR='' OLDIFS=$IFS IFS=' ' for i in $LINE; do if [[ $i = "jp_directory" ]]; then COUNT1=1 STR=`echo "$STR $i"` elif [[ $COUNT1 = "1" ]] && [[ $i = "<>" ]] && [[ $COUNT2 = "2" ]]; then STR=`echo "$STR "` COUNT2=`expr $COUNT2 + 1` elif [[ $COUNT1 > "0" ]] && [[ $i = "<>" ]] && [[ $COUNT2 < "2" ]]; then COUNT2=`expr $COUNT2 + 1` STR=`echo "$STR $i"` else STR=`echo "$STR $i"` fi done IFS=$OLDIFS echo $STR >>TEMP else echo $LINE >>TEMP fi done mv TEMP jp.sql.old done exit $ cat jp.sql this is a file tof testing the shell scripting with bulk <> is a test <> pattern <> <> <> <> jp_directory <> <> jp_directory <> <> that <> is <> jp_directory <> <> to be filled in a test of programming in the demo for the beginner in unix ./bfe.sh /* Let us verify the modified file jp.sql.old */ $ cat jp.sql.old this is a file tof testing the shell scripting with bulk <> is a test <> pattern <> <> <> <> jp_directory <> <> jp_directory <> <> that is <> jp_directory <> <> to be filled in a test of programming in the demo for the beginner in unix /* Happy scripting */