Skip to main content
Specifically use gawk in the command.
Source Link
Stephen Kitt
  • 481k
  • 59
  • 1.2k
  • 1.4k

With GAWK:

awkgawk 'BEGINFILE { if (ERRNO) nextfile } (FNR==1) { print FILENAME }' file_?

In the BEGINFILE block, ERRNO is empty if the file was opened successfully, and nextfile can be used to skip to the next and avoid bailing out with an error.

I don’t think this is supported in other implementations of AWK.

Portably, you can iterate over all the arguments, check whether they point to an unreadable file, and if so, remove them from the arguments before AWK starts processing them; the GAWK manual has an example implementation. This is racy however since a file checked using this loop may become unreadable (or vice versa) before AWK gets round to processing it.

With GAWK:

awk 'BEGINFILE { if (ERRNO) nextfile } (FNR==1) { print FILENAME }' file_?

In the BEGINFILE block, ERRNO is empty if the file was opened successfully, and nextfile can be used to skip to the next and avoid bailing out with an error.

I don’t think this is supported in other implementations of AWK.

Portably, you can iterate over all the arguments, check whether they point to an unreadable file, and if so, remove them from the arguments before AWK starts processing them; the GAWK manual has an example implementation. This is racy however since a file checked using this loop may become unreadable (or vice versa) before AWK gets round to processing it.

With GAWK:

gawk 'BEGINFILE { if (ERRNO) nextfile } (FNR==1) { print FILENAME }' file_?

In the BEGINFILE block, ERRNO is empty if the file was opened successfully, and nextfile can be used to skip to the next and avoid bailing out with an error.

I don’t think this is supported in other implementations of AWK.

Portably, you can iterate over all the arguments, check whether they point to an unreadable file, and if so, remove them from the arguments before AWK starts processing them; the GAWK manual has an example implementation. This is racy however since a file checked using this loop may become unreadable (or vice versa) before AWK gets round to processing it.

Idiomatic ERRNO check. Point to the GAWK manual example, thanks ilkkachu!
Source Link
Stephen Kitt
  • 481k
  • 59
  • 1.2k
  • 1.4k

With GAWK:

awk 'BEGINFILE { if (ERRNO != "") nextfile } (FNR==1) { print FILENAME }' file_?

In the BEGINFILE block, ERRNO is empty if the file was opened successfully, and nextfile can be used to skip to the next and avoid bailing out with an error.

I don’t think this is supported in other implementations of AWK.

Portably, you can iterate over all the arguments, check whether they point to an unreadable file, and if so, remove them from the arguments before AWK starts processing them; the GAWK manual has an example implementation. This is racy however since a file checked using this loop may become unreadable (or vice versa) before AWK gets round to processing it.

With GAWK:

awk 'BEGINFILE { if (ERRNO != "") nextfile } (FNR==1) { print FILENAME }' file_?

In the BEGINFILE block, ERRNO is empty if the file was opened successfully, and nextfile can be used to skip to the next and avoid bailing out with an error.

I don’t think this is supported in other implementations of AWK.

With GAWK:

awk 'BEGINFILE { if (ERRNO) nextfile } (FNR==1) { print FILENAME }' file_?

In the BEGINFILE block, ERRNO is empty if the file was opened successfully, and nextfile can be used to skip to the next and avoid bailing out with an error.

I don’t think this is supported in other implementations of AWK.

Portably, you can iterate over all the arguments, check whether they point to an unreadable file, and if so, remove them from the arguments before AWK starts processing them; the GAWK manual has an example implementation. This is racy however since a file checked using this loop may become unreadable (or vice versa) before AWK gets round to processing it.

Source Link
Stephen Kitt
  • 481k
  • 59
  • 1.2k
  • 1.4k

With GAWK:

awk 'BEGINFILE { if (ERRNO != "") nextfile } (FNR==1) { print FILENAME }' file_?

In the BEGINFILE block, ERRNO is empty if the file was opened successfully, and nextfile can be used to skip to the next and avoid bailing out with an error.

I don’t think this is supported in other implementations of AWK.