How to skip a directory in awk?

后端 未结 2 1776
攒了一身酷
攒了一身酷 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<ARGC;i++) {
            if ( (getline line < ARGV[i]) <= 0 ) {
                print "Skipping:", ARGV[i], ERRNO
                delete ARGV[i]
            }
            close(ARGV[i])
        }
    }
    { print FILENAME, $0 }
    
    $ awk -f tst.awk tmp/*
    Skipping: tmp/dir Is a directory
    tmp/bar line 1
    tmp/bar line 2
    tmp/foo line 1
    
    $ awk --posix -f tst.awk tmp/*
    Skipping: tmp/dir
    tmp/bar line 1
    tmp/bar line 2
    tmp/foo line 1
    

    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.

    0 讨论(0)
  • 2021-01-12 19:50

    I would simply avoid to pass directories to awk since even POSIX says that all filename args must be text files.

    You can use find for traversing the directory:

    find PATH -type f -exec awk 'program' {} +
    
    0 讨论(0)
提交回复
热议问题