sql - Replace text from select statement from one file to another -


i have bunch of views database , need update select statements within each view.

i have select statements in files called viewname.txt in 1 dir , in sub dir called sql; have views viewname.sql. want run script take text viewname.txt , replace select statement in correct viewname.sql in sql sub dir.

i have tried append text after select in each .sql file:

for in */*;   if ["../$(basename "${i}")" == "$(basename "${i}")"]       sed '/select/a "$(basename "$i" .txt)"' "$(basename "$i" .sql)"   fi done 

any assistance appreciated!

dickie

this awk answer that's close - output placed in sql directory under corresponding "viewname.sql.new" files.

#!/usr/bin/awk -f  # absorb whole viewname.txt file arr when first line read filename ~ /\.txt$/ && filename != last_filename {     last_filename = filename      # viewname part of file name     split( filename, file_arr, "." )      while( getline file_data <filename > 0 ) {         old_data = arr[ file_arr[ 1 ] ]         arr[ file_arr[ 1 ] ] = \             old_data (old_data == "" ? "" : "\n") file_data     }     next }  # process each line of sql/viewname.sql files filename ~ /\.sql$/ {     # strip "/sql" front of filename lookup in arr     split( substr( filename, 5 ), file_arr, "." )     if( file_arr[ 1 ] in arr ) {         if( $0 ~ /select/ )             print arr[ file_arr[ 1 ] ] > filename ".new"         else             print $0 > filename ".new"     } } 

i put file called awko , chmod +x , ran following

awko *.txt sql/* 

you'll have mv new files place, it's close can right now.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -