[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [bug-gawk] The best way to test the first n character in a file
From: |
Stephane Chazelas |
Subject: |
Re: [bug-gawk] The best way to test the first n character in a file |
Date: |
Thu, 19 Sep 2019 14:35:40 +0100 |
User-agent: |
NeoMutt/20171215 |
2019-09-18 11:35:15 -0500, Peng Yu:
[...]
> I want to test if the first n character of a binary file equals some
> given string. I am not sure if awk is good at this. It seems that
> `head` plus shell code should be easier. Is there a good way to do so
> in awk? Thanks.
[...]
There's a bit of a contradiction in that statement, unless you
meant "byte" instead of "character". binary file usually means
non-text files, so where bytes (or sequence of bytes) are not
meant to be interpreted as characters.
With GNU awk, you can do:
LC_ALL=C gawk '
BEGIN {
bytesequence="\x7f" "ELF"
RS = ".{" length(bytesequence) "}"
}
{
if (RT == bytesequence) print FILENAME" is an ELF file"
nextfile
}' /bin/ls /bin/zgrep
--
Stephane