bash 将 hexdump 转换回文本的命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46990378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 16:33:36  来源:igfitidea点击:

Command to convert hexdump back to text

bashcommand-linehexdumpxxd

提问by Majid Fouladpour

I am using Catfish to find files containing certain text. Some of the files found instead of plain text contain something which looks like a hex dump:

我正在使用 Catfish 来查找包含特定文本的文件。找到的一些文件而不是纯文本包含看起来像十六进制转储的内容:

3c3f 7068 700a 0a66 756e 6374 696f 6e20
7573 6572 5f65 7869 7374 7328 2475 6e29
207b 0a09 7265 7475 726e 2074 7275 653b
0a7d 0a0a 0a3f 3e00 0000 0000 0000 0000

I.e. always 8 columns and each row containing 16 bytes. Line one above should be

即总是 8 列,每行包含 16 个字节。上面的第一行应该是

<?php
function

Is there a command to unhexthe file and print the content?

是否有命令可以解开文件并打印内容?

回答by etopylight

Use xxdwith the revert option -r

使用xxd与还原选项-r

xxd -p -r file.txt

gives a result of

结果是

<?php

function user_exists($un) {
        return true;
}


?>

回答by Cyrus

With bash:

使用 bash:

for block in $(cat file); do 
  [[ $block =~ (..)(..) ]] && echo -en "\x${BASH_REMATCH[1]}\x${BASH_REMATCH[2]}"
done

Output:

输出:

<?php

function user_exists($un) {
        return true;
}


?>

回答by Darby_Crash

echo -e "$(sed -r 's@\s*([a-f0-9][a-f0-9])\s*@\U@g' < test.txt | tr -d '\n')"

The sed regex capture couple of two hexadecimal characters and zero or more spaces from the begin and from the end of the match and convert it to unicode character removing spaces (g is global sostitution and -r enable extended regex).

sed 正则表达式从匹配的开头和结尾捕获两个十六进制字符和零个或多个空格,并将其转换为 unicode 字符删除空格(g 是全局 sostitution 和 -r 启用扩展正则表达式)。

tr remove line feed present in the file

tr 删除文件中存在的换行符

echo -e translate unicode to ascii character

echo -e 将 unicode 转换为 ascii 字符