我在bash脚本中使用这段代码读取包含几个十六进制字符串的文件,进行一些替换,然后将其写入新文件。大约300兆需要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.021suser 29m17.640ssys 15m1.070s
sed 's,[0-9A-Z]\{2\},\\\\x&,g' ${in_file} | while read line; do
    printf '%b\000\000' ${line}
done >> ${out_file}

真正的18m50.288suser 8m46.400ssys 10m10.170s
export LANG=C
sed 's/$/0000/' ${in_file} | xxd -r -ps >> ${out_file}

实际0m31.528suser 0m1.850ssys 0m29.450s

最佳答案

你需要Vim附带的xxd命令。

export LANG=C
sed 's/$/0000/' ${in_file} | xxd -r -ps > ${out_file}

关于bash - 可以更快地完成此操作吗(读取文件,替换[sed],写入新文件),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3694503/

10-12 00:53
查看更多