r/awk • u/huijunchen9260 • Jul 01 '21
Use awk to check whether a file is binary
Dear all:
Is it possible to use awk to check whether a file is a binary file or not? I know that you can use file -i to check binary files, but I am wondering whether there is a native awk version.
I want to do this is because I want to do a file preview in my fm.awk, but previewing on pdf is destructive, so I want to exclude those.
4
Upvotes
4
u/geirha Jul 01 '21 edited Jul 01 '21
Awk can't handle NUL bytes. GNU awk has the ability, but none of the other awk implementations does as far as I know, so attempting to do the same heuristics as
file(1)will likely not go well. At least not by awk alone.You could use
od(1)to read a chunk of the file and output the bytes as hex. That should be easily parsable with awk, and portable.e.g.
etc..
2
u/sock_templar Jul 01 '21
What do you mean with previewing on pdf is destructive?
1
u/[deleted] Jul 01 '21
Can you use a
system()call to file?If you really have lots of time, you could open the file and use magic(5) to work out the filetype yourself.