0

I have a huge file (dump of swap-file) with random bytes data. I need to get files starts and ends with exact bytes in hexadecimal, which I know. For example jpeg starts with FF D8 FF E0 00 10 4A 46 49 46 and ens with FF D9. Moreover, I need to do this using bash script. Can You give some advices how to do this?

Mikegn
  • 11
  • 3

2 Answers2

1

The basic advice is: do not use bash for this. Although it is possible, bash is not the tool for this. You are better of writing short C-code.

If you really insist on doing it in bash,

while LANG=C IFS= read -r -d '' -n 1 char ;do
    #do your test for the next byte
done

reads the bytes one-by-one.

Alternatively, you may use the output of od -x to pass through some loops.

But that is really only if you must use bash.

Ljm Dullaart
  • 4,273
  • 2
  • 14
  • 31
  • I have already done this using C# and python, but unfortunately I need to do this using bash =( – Mikegn Jun 29 '18 at 20:53
0

I wouldn't use Bash for this. If you must use bash, then maybe try regex?

JPG_REGEX='FF D8 FF E0 00 10 4A 46 49 46 [\w|\s]+ FF D9' #this matches any jpeg file

echo <name of your file here> | grep -P -q $JPG_REGEX

Check out this answer.

Fabián Montero
  • 1,613
  • 1
  • 16
  • 34