这是一个接近的 awk 答案 - 输出放置在相应的“viewname.sql.new”文件下的 sql 目录中。
#!/usr/bin/awk -f
# absorb the whole viewname.txt file into arr when the first line is read
FILENAME ~ /\.txt$/ && FILENAME != last_filename {
last_filename = FILENAME
# get the viewname part of the 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 the sql/viewname.sql files
FILENAME ~ /\.sql$/ {
# strip the "/sql" from the front of FILENAME for 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"
}
}
我把它放到一个名为的文件中awko
,chmod +x
然后像下面这样运行它
awko *.txt sql/*
您必须将新文件移动到位,但它已经尽可能接近了。