Feed unzipped files from all subdirectories into awk

100 Views Asked by At

I want awk to process all files in unzipped form from all sub-directories.

This is what I've tried: It works only for top level directory files.

for file in *
do
    awk -v f="$file" 'NF > 0 {print f; nextfile}' <(gunzip -cf $(find . -type f) "$file")
done

(My goal is not to just print non empty file names using awk, I just gave this as an example)

3

There are 3 best solutions below

5
markp-fuso On BEST ANSWER

Assumptions:

  • we want awk to print the name of the zipped file
  • in the case of a zipped file that consists of a group of files we (still) only want to print the name of the zipped file, otherwise OP will need to provide more details on how to process the zipped file

One idea:

while read -r file
do
    awk -v f="$file" 'NF>0 {print f; nextfile}' <(gunzip -cf "$file")
done < <(find . -type f)
3
Devesh Nair On
find . -type f -exec awk 'NF > 0 {print FILENAME}' {} \;

This might work For your second question:

find . -type f -name "*.gz" -exec sh -c 'gunzip -c "$1" | awk "NF > 0 {print \"$1\"}"' _ {} \;
6
Kaz On

TXR Lisp:

(ftw "."                                          ;; POSIX nftw function
     (lambda (path type . rest)
       (if (eql type ftw-f)                       ;; if regular file
         (with-stream (s (open-file path "z"))    ;; open with "z" option
           (typecase s                            ;; switch on type of stream
             (gzip-stream                         ;; gzip stream: it's a zipped file 
               (put-line `gzipped file @path`))
             (t                                   ;; any other type: it isn't.
               (put-line `not gzipped file @path`)))))))