How to skip a directory in awk?

后端 未结 2 1775
攒了一身酷
攒了一身酷 2021-01-12 19:33

Say I have the following structure of files and directories:

$ tree
.
├── a
├── b
└── dir
    └── c

1 directory, 3 files

That is, two file

2条回答
  •  离开以前
    2021-01-12 19:50

    If you wanted to safeguard your script from other people mistakenly passing a directory (or anything else that's not a readable text file) to it, you could do this:

    $ ls -F tmp
    bar  dir/  foo
    
    $ cat tmp/foo
    line 1
    
    $ cat tmp/bar
    line 1
    line 2
    
    $ cat tmp/dir
    cat: tmp/dir: Is a directory
    
    $ cat tst.awk
    BEGIN {
        for (i=1;i

    Per POSIX getline returns -1 if/when it fails trying to retrieve a record from a file (e.g. unreadable file or file does not exist or file is a directory), you just need GNU awk to tell you which of those failures it was by the value of ERRNO if you care.

提交回复
热议问题