Pages

17 September, 2014

Pushing uncompressed and gzip-compressed files into awk

I was just wondering how to best handle compressed and uncompressed files together in my CSV-processing bash/awk scripts. Here is a very simple solution:

#!/usr/bin/env bash

file=my-data.csv.gz

kat(){
  case $file in
    *.gz) gzip -dc $file;;
    *)    cat      $file;;
  esac
}


kat $file | awk '{/* do something */}'

I would say this is really lean code. Even if bash has its restrictions and I despised it in the past, I
have come to love it over the past years.
 
PS: I know there are more sophisticated tools, such as `file` and `zcat`. However, they are not available in the default GitBash on Windows, which I am still using at work for many tasks.


No comments: