python - 优化 shell 脚本 (bash) 以提高性能

标签 python linux perl bash shell

我有一个用于处理文本文件的 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

最佳答案

要使此脚本更快,您必须尽量减少对外部命令的调用并尽可能使用 bash。

  1. 阅读this article知道什么是无用的命令。

  2. 阅读this article了解如何使用 bash 来操作字符串。

  3. 将重复值(var1、var2、var4)赋值替换为单个值。

在优化 cut 时你可以替换

var1=`echo $line | cut -d '*' -f 1`

var1="${line%%\**}"

var5=`echo $line | cut -d '*' -f 2`

var5="${line%\**}"
var5="${var5##*\*}"

也许它不是那么可读,但比 cut 快得多。

还有

 echo $line | grep -v '\/' | grep -v '\-\-' | grep -v '#'

可以替换成类似的东西:

 if [[ "$line" =~ ([/#]|--) ]]; then :; else 
    # all code inside "if [ $? -eq 0 ]"
 fi

关于python - 优化 shell 脚本 (bash) 以提高性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16515699/

相关文章:

c - 是否可以将部分共享二级缓存分配给不同的内核

json - 如何避免使用 Perl JSON 模块在数字周围加上引号?

perl - 在 Perl 中发送 SMTP 电子邮件的更快方法?

python - python 的 sphinx autodoc 没有显示任何内容(在 readthedocs 上)

python - 使用 fixture 时 pytest capsys 未捕获标准输出

python - 使用 SQL Alchemy 声明性基础处理元类冲突

Perl:如何动态创建对象?

python - Pandas 、应用函数和 lambda

linux 托管权限共享服务器

linux - 如何从同一终端中停止正在运行的 Docker 容器?