【发布时间】:2011-04-01 13:55:49
【问题描述】:
我需要将 uTorrent 风格的 ipfilter.dat 转换为 bluetack 风格的 ipfilter 文件,并编写了这个 shell 脚本来实现这一点:
#!/bin/bash
# read ipfilter.dat-formatted file line by line
# (example: 000.000.000.000-008.008.003.255,000,Badnet
# - ***here, input file's lines/fields are always the same length***)
# and convert into a bluetack.co.uk-formatted output
# (example: Badnet:0.0.0.0-8.8.3.255
# - fields moved around, leading zeros removed)
while read record
do
start=`echo ${record:0:15} | awk -F '.' '{for(i=1;i<=NF;i++)$i=$i+0;}1' OFS='.'`
end=`echo ${record:16:15} | awk -F '.' '{for(i=1;i<=NF;i++)$i=$i+0;}1' OFS='.'`
echo ${record:36:7}:${start}-${end}
done < $1
但是,在 2000 行的输入文件上,此脚本平均需要 10(!) 秒才能完成 - 仅需 200 行/秒。
我确信使用 sed 也可以达到同样的效果,而且 sed-version 可能要快得多。
是否有 sed-guru 为这种固定位置替换提出解决方案?
您也可以随意提出其他语言的解决方案 - 例如,我会喜欢测试 Python 或 C 版本。我们也欢迎更高效的 shell/bash 版本。
【问题讨论】:
标签: performance bash sed awk