我在我的 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.640s
sys 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.288s
用户 8m46.400s
sys 10m10.170s
export LANG=C
sed 's/$/0000/' ${in_file} | xxd -r -ps >> ${out_file}
真实 0m31.528s
用户 0m1.850s
sys 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/