在大文件上将十六进制转换为二进制(Convert Hex to Binary on large file)
我有一个约230万行的文本文件。 每行包含一个64个字符的十六进制字符串。 我试图逐行读取文件并将十六进制字符串转换为二进制并输出到文件。 我在下面用bash编写了这个简单的循环,但我知道它不是最优的,它将需要永远完成。
有没有更快的方法,例如使用awk? 最好使用perl? 我只需要更快的东西。
cat /tmp/hexFile.log | while read line do bin=$(echo "obase=2; ibase=16; $line" | bc ) bin=`echo $bin | sed 's/\\\ //g'` echo $bin >> /tmp/binOutput.log doneI have a text file with ~2.3 million lines. Each line contains a 64 character hexadecimal string. I am trying to read in the file line by line and convert the hex string to binary and output to a file. I wrote this simple loop below in bash, but I know it is not optimal and it will take forever to complete.
Is there a faster way, using awk for instance? Better to use perl? I just need something that is much faster.
cat /tmp/hexFile.log | while read line do bin=$(echo "obase=2; ibase=16; $line" | bc ) bin=`echo $bin | sed 's/\\\ //g'` echo $bin >> /tmp/binOutput.log done最满意答案
这对我有用。
#!/usr/bin/perl while (<>) { chomp; for (my $i = 0; $i < length($_); $i += 1) { printf('%04b', hex(substr($_, $i, 1))) } print "\n"; }This works for me.
#!/usr/bin/perl while (<>) { chomp; for (my $i = 0; $i < length($_); $i += 1) { printf('%04b', hex(substr($_, $i, 1))) } print "\n"; }更多推荐
发布评论