bug-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Native file exist check


From: arnold
Subject: Re: Native file exist check
Date: Sun, 07 Jun 2020 00:06:01 -0600
User-agent: Heirloom mailx 12.5 7/5/10

Had you looked in the manual, you'd have found a similar
function for doing this kind of test.

Note that "getline reading too much ... processing a binary file"
indicates a misunderstanding as well. Gawk is not intended to
process binary files, and it's a bad idea to try to do so,
in any case.  (Use perl or python if you want to do that.)

The other mails in this thread have otherwise answered the
question.  The idea of using RS to ensure a small line is
a good one. To that end I'd suggest RS = ".{1}".

Arnold

Peng Yu <pengyu.ut@gmail.com> wrote:

> Hi,
>
> https://stackoverflow.com/questions/29284472/awk-check-file-exists
>
> The following solution is shown above. But getline could potentially
> read too much (for example processing a binary file in which a line
> can be very long). Is there a more efficient awk native solution for
> checking whether a file exist?
>
> function file_exists(file) {
>   n=(getline _ < file);
>   if (n > 0) {
>     print "Found: " file;
>     return 1;
>   } else if (n == 0) {
>     print "Empty: " file;
>     return 1;
>   } else {
>     print "Error: " file;
>     return 0;
>   }
> }
>
>
>
> -- 
> Regards,
> Peng



reply via email to

[Prev in Thread] Current Thread [Next in Thread]