5

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:

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

<?php
function

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

Cyrus
  • 84,225
  • 14
  • 89
  • 153
Majid Fouladpour
  • 29,356
  • 21
  • 76
  • 127

3 Answers3

8

Use xxd with the revert option -r

xxd -p -r file.txt

gives a result of

<?php

function user_exists($un) {
        return true;
}


?>
etopylight
  • 1,239
  • 1
  • 10
  • 15
3

With 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;
}


?>
Cyrus
  • 84,225
  • 14
  • 89
  • 153
0
echo -e "$(sed -r 's@\s*([a-f0-9][a-f0-9])\s*@\\U\1@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).

tr remove line feed present in the file

echo -e translate unicode to ascii character

Darby_Crash
  • 446
  • 3
  • 6
  • Please add an explanation to improve the quality of your answer and help people understand how your suggested solution is working. – Ivan Gabriele Oct 28 '17 at 20:57