【发布时间】:2010-09-12 10:34:23
【问题描述】:
我在我的 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
【问题讨论】:
-
显示输入文件的示例和输出格式
-
在调用它之前运行它以更改为快速 C 语言环境:export LANG=C
-
@LatinSuD。不知道你的意思。
标签: bash file sed substitution