unix scrip的增强

2024-10-02 02:34:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个算法,可以屏蔽用户输入的列的数据。下面的功能是为同样的目的而设计的。当脚本被执行时,大约需要4个小时来屏蔽100000条记录。客户希望在10分钟内达到同样的效果。您能否建议我如何实现以下内容,以便提高其性能。不管怎样,只要不改变算法就可以改变函数。你知道吗

data_mask() {
col_val=$1
l_ret_str=""
l_an=0
l_lp=0
l_mod=0
absnum=0
austart=65
auend=90
aclsize=26
alstart=97
alend=122
nstart=48
nend=57
nclsize=10
l_lp=`expr length "$col_val"`
if [[ $l_lp -ne 0 ]]; then
for i in `eval "echo {1..$l_lp}"`
do
single_char=$(SUBSTR "$col_val" $i)
ascii_num_val=$(ASCII "$single_char")
l_mod=$((l_mod+ascii_num_val))
done
l_mod=$((l_mod % nclsize))
for i in `eval "echo {1..$l_lp}"`
do
single_char=$(SUBSTR "$col_val" $i)
ascii_num_val=$(ASCII "$single_char")
l_an=$ascii_num_val
tempvar=$((l_an - l_lp - l_mod - i))
absnum=$(ABS $tempvar)
if [[ $l_an -ge $austart && $l_an -le $auend ]]; then
tempmodval=$((absnum % aclsize))
tempasciival=$((austart + tempmodval))
l_ret_str=$l_ret_str$(CHR $tempasciival)
elif [[ $l_an -ge $alstart && $l_an -le $alend ]]; then
tempmodval=$((absnum % aclsize))
tempasciival=$((alstart + tempmodval))
l_ret_str=$l_ret_str$(CHR $tempasciival)
elif [[ $l_an -ge $nstart && $l_an -le $nend ]]; then
tempmodval=$((absnum % nclsize))
tempasciival=$((nstart + tempmodval))
l_ret_str=$l_ret_str$(CHR $tempasciival)
else
tempmodval=$((absnum % nclsize))
tempasciival=$((austart + tempmodval))
l_ret_str=$l_ret_str$(CHR $tempasciival)
fi
done
fi
echo "$l_ret_str"
}

此处col_val=$1输入者用户.if用户输入2,我们的代码将屏蔽第二列。我们通过下面调用上面的函数。你知道吗

    while read p; do
if [[ $line -le $skip_line ]]; then
echo "$p" >> $outputfile
else
pre_str=`echo $p | cut -d'|' -f1-$((colnum - 1))`
column_value=`echo $p | cut -d'|' -f$colnum`
post_str=`echo $p | cut -d'|' -f$((colnum + 1))-$totalcol`
echo "column_value=$column_value"
maskvalue=$(data_mask "$column_value")
echo $pre_str"|"$maskvalue"|"$post_str >> $outputfile
fi
line=$((line + 1))
done <$temp_outputfile

这里我们把文件分成三部分。然后调用我们的函数。这里skipline是代码应该包含的行数跳过.eg标题。 所以如果输入是

id|name|dept
11|Shrut|consultant
12|wipro|HR
13|capgemini|IT

那么输出应该如下所示。你知道吗

id|name|dept
11|sqmbr|consultant
12|itzaw|HR
13|khvlipkoi|IT

请提出一些建议。如果你需要一些澄清,我会在评论中提供,但请不要把它搁置。我必须提高执行速度,而不改变用data\u mask()编写的算法。函数可以更改,但算法不能更改。 我同样期待你的帮助。你知道吗


Tags: 函数echo算法anmodcolvallp
2条回答

Bash在执行循环时性能不是很好。 既然您标记了问题python,那么python应该可以吗?你知道吗

def data_mask(col_val):
    mod = len(col_val) + sum(map(ord, col_val)) % 10
    result = ""
    for i, ch in enumerate(col_val, mod + 1):
        absnum = abs(ord(ch) - i)
        if 'A' <= ch <= 'Z':
            ch = chr(ord('A') + absnum % 26)
        elif 'a' <= ch <= 'z':
            ch = chr(ord('a') + absnum % 26)
        elif '0' <= ch <= '9':
            ch = chr(ord('0') + absnum % 10)
        else:
            ch = chr(ord('A') + absnum % 10)
        result += ch
    return result

while open(temp_outputfile) as lines:
    while open(outputfile, 'w') as output:
        output.write(next(lines))
        for line in lines:
            pre, col_val, post = line.split('|', 2)
            output.write("{}|{}|{}".format(pre, data_mask(col_val), post))

@丹尼尔, 很好的代码,为同一个问题写了类似的东西, 但既然你的密码更好了,我就不发了, 没有想过使用枚举,而是循环遍历它们。你知道吗

现在我将返回并重新考虑我的许多代码,以包括枚举的自由。你知道吗

相关问题 更多 >

    热门问题