Essential AWK Commands for Next Generation Sequence Analysis

Here are the few essential awk command line scripts for next generation sequence analysis.

Users need latest version of gawk to run commands with bitwise operations. Most of the Linux distributions come with gawk. However OSX users have to install it from here

Count number of reads in a FastQ file

Convert FastQ to FastA


Get reads matching a sequence pattern and convert them to FastA

This will get all reads with EcoRI cleavage (GAATTC)

Separate reads based on their length

This will print reads thats are 75bp or more in length.

Printing the above output in FastA format


Get All header lines from a SAM file


Get all reads – excluding headers


Get all unmapped

Get all mapped


Count unmapped

Count mapped


Convert all unmapped reads into fasta format

Convert all mapped reads into fasta format

Convert all reads into fasta format


  • Quan Gu


  • David

    correct me if I am mistaken, but I think there is a mistake in command which is supposed to separate reads in fastq file by their length and then output them in fasta format:

    awk ‘NR%4==1{a=substr($0,2);} NR%4==2{b=$0} NR%4==3{c=$0} NR%4==0&&length(b)>=75{print a”n”b”n”c”n”$0;}’ file.fq

    I think that b and c strings are not needed and output print has to be adjusted as well. To get the output in fasta format I think it would have to be something like this:

    awk ‘NR%4==1{a=substr($0,2);} NR%2==0&&length($0)>=75{print “>”a”n”$0;}’ file.fa

    Thank you for great website 🙂