[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: |
Neil R. Ormos |
Subject: |
Re: [bug-gawk] The best way to test the first n character in a file |
Date: |
Wed, 18 Sep 2019 17:43:45 -0500 (CDT) |
Peng Yu wrote:
> 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.
When I have needed to do this, I set RS to match the count of initial bytes to
be inspected -- i.e., if I need to inspect the first ten bytes of the file, I
set RS=".........." -- and then attempt to read one record from the file into a
variable using getline. If the read was successful, then RT will contain the
initial bytes. I use the C locale; I haven't tried others.