linuxbashgzipbzip2fastq

How to merge zcat and bzcat in a single function


I would like to build a little helper function that can deal with fastq.gz and fastq.bz2 files.

I want to merge zcat and bzcat into one transparent function which can be used on both sorts of files:

zbzcat example.fastq.gz
zbzcat example.fastq.bz2


zbzcat() {
  file=`echo $1 | `
## Not working
  ext=${file##*/};
  
  if [ ext == "fastq.gz" ]; then
    exec gzip -cd "$@"  
  else
    exec bzip -cd "$@"  
  fi
}

The extension extraction is not working correctly. Are you aware of other solutions


Solution

  • These are quite a lot of problems:

    By the way: You don't have to extract the extension at all, when using pattern matchting:

    zbzcat() {
      file="$1"
      case "$file" in
        *.gz) gzip -cd "$@";;
        *.bz2) bzip -cd "$@";;
        *) echo "Unknown file format" >&2;;
      esac
    }
    

    Alternatively, use 7z x which supports a lot of formats. Most distributions name the package p7zip.