我在bash脚本中使用这段代码读取包含几个十六进制字符串的文件,进行一些替换,然后将其写入一个新文件。300 Mb大约需要30分钟。
我在想这能不能更快些?
sed 's,[0-9A-Z]\{2\},\\\\x&,g' ${in_file} | while read line; do
printf "%b" ${line} >> ${out_file}
printf '\000\000' >> ${out_file}
done更新:
我做了一些测试,得到了以下结果:
获胜者是:
sed 's,[0-9A-Z]\{2\},\\\\x&,g' ${in_file} | while read line; do
printf "%b" ${line} >> ${out_file}
printf '\000\000' >> ${out_file}
done实44m27.021s
用户29m17.640
sys 15m1.070
sed 's,[0-9A-Z]\{2\},\\\\x&,g' ${in_file} | while read line; do
printf '%b\000\000' ${line}
done >> ${out_file}实际18m50.288s
用户8m46.400s
sys 10m10.17ps
export LANG=C
sed 's/$/0000/' ${in_file} | xxd -r -ps >> ${out_file}实0m31.528s
用户0m1.850
sys 0m29.450
发布于 2010-09-12 11:12:08
您需要Vim附带的xxd命令。
export LANG=C
sed 's/$/0000/' ${in_file} | xxd -r -ps > ${out_file}发布于 2010-09-12 10:53:12
这是缓慢的,因为循环在bash。如果您可以让sed/awk/perl/etc来执行循环,它将更快。不过,我看不出你怎么能用sed或awk做这件事。对于perl来说,这可能很容易,但是我不知道足够的perl来回答这个问题。
至少,您应该能够通过重构您需要的内容来节省一点时间:
sed 's,[0-9A-Z]\{2\},\\\\x&,g' ${in_file} | while read line; do
printf '%b\000\000' ${line}
done >> ${out_file}至少这样,每次迭代运行printf一次,只打开/关闭${out_file}一次。
发布于 2010-09-12 10:56:27
切换到一种完整的编程语言?下面是一个Ruby一行:
ruby -ne 'print "#{$_.chomp.gsub(/[0-9A-F]{2}/) { |s| s.to_i(16).chr }}\x00\x00"'https://stackoverflow.com/questions/3694503
复制相似问题