我有一个用于处理文本文件的 bash 脚本:
#/bin/bash
dos2unix sourcefile.txt
cat sourcefile.txt | grep -v '\/' | grep -v '\-\-' | grep -v '#' | grep '[A-Za-z]\*' > modified_sourcefile.txt
mv modified_sourcefile.txt sourcefile.txt
#
# Read the sourcefile file one line by line and iterate...
#
while read line
do
echo $line | grep -v '\/' | grep -v '\-\-' | grep -v '#'
if [ $? -eq 0 ]
then
# echo "Current Line is " $line ";"
char1=`echo ${line:0:1}`
# echo "1st char is " $char1
if [ -n "$char1" ]
# if a blank-line, neglect the line.
then
# echo "test passed"
var1=`echo $line | cut -d '*' -f 1`
var2=`echo $line | cut -d '*' -f 1`
var3=`echo $line | cut -d - -f 1`
var4=`echo $line | cut -d '*' -f 1`
var5=`echo $line | cut -d '*' -f 2`
var6=`echo $line | cut -d - -f 1`
var7=`echo $line | cut -d '*' -f 3 `
table1sql="INSERT IGNORE INTO table1 (id,name,active_yesno,category,description,
last_modified_by,last_modified_date_time) SELECT ifnull(MAX(id),0)+1,'$var1',1,
'$var2','$var3','admin',NOW() FROM table1;"
echo $table1sql >> result.txt
privsql="INSERT IGNORE INTO table2 (id,name,description,active_yesno,group_code,
last_modified_by,last_modified_date_time) SELECT ifnull(MAX(id),0)+1,'$var1',
'$var3',1,'$var2','admin',NOW() FROM table2;"
echo $privsql >> result.txt
table1privmapsql="INSERT IGNORE INTO table1_table2_map (id,table1_id,table2_id,
last_modified_by,last_modified_date_time) SELECT ifnull(MAX(id),0)+1,
(select id from table1 where name='$var1'),(select id from table2 where name='$var1'),'admin',NOW() FROM table1_table2_map;"
echo $table1privmapsql >> result.txt
privgroupsql="INSERT IGNORE INTO table2_group (id,name,category,active_yesno,last_modified_by,
last_modified_date_time) SELECT ifnull(MAX(id),0)+1,'tablegrp','$pgpcode',1,'admin',NOW() FROM table2_group;"
echo $privgroupsql >> result.txt
privprivgrpsql="INSERT IGNORE INTO table2_table2group_map (id,table2_id,table2_group_id,
last_modified_by,last_modified_date_time) SELECT ifnull(MAX(id),0)+1,
(select id from table2 where name='$var1'),(select id from table2_group where name='tablegrp'),'admin',NOW() FROM table2_table2group_map;"
echo $privprivgrpsql >> result.txt
rolesql="INSERT IGNORE INTO role (id,name,active_yesno,security_domain_id,last_modified_by,last_modified_date_time)
SELECT (select ifnull(MAX(id),0)+1 from role),'$rolename',1, sd.id ,'admin',NOW()
FROM security_domain sd WHERE sd.name = 'General';"
echo $rolesql >> result.txt
fi
fi
done < "sourcefile.txt"
问题是 sourcefile.txt 有超过 11000 行。所以大约需要 25 分钟才能完成 :-( 。
有更好的方法吗?
sourcefile.txt 的内容:
AAA-something*LOCATION-some_where*ABC