AWK

USEFUL LINKS

10 Awk Tips, Tricks and Pitfalls
Control Statements in AWK
Signs in AWK

COMMANDS

IF

if(conditional-expression1)
    action1;
else if(conditional-expression2)
    action2;
else if(conditional-expression3)
    action3;
    .
    .
else
    action n;

EXAMPLE
$ awk '{
if ($3 >=35 && $4 >= 35 && $5 >= 35)
    print $0,"=>","Pass";
else
    print $0,"=>","Fail";
}' student-marks
Jones 2143 78 84 77 => Pass
Gondrol 2321 56 58 45 => Pass
RinRao 2122 38 37 => Fail
Edwin 2537 87 97 95 => Pass
Dayan 2415 30 47 => Fail

HANDY SCRIPTS for AWK

substitute atom name in a pdb

This is useful when you have ligand, which its atom type is not recognizable by other program like amber.
USAGE: AWK -f subtitution.awk pdb >new_pdb

#!/bin/awk 
{
 if ($4=="LIG") {
    gsub("C1 ","CA ")
    gsub("C2 ","CA ")
    gsub("C3 ","CA ")
    gsub("C4 ","CA ");
    if (length($3)==2){
       printf "%s   %4d  %-1s  %s   %s%12.3f%8.3f%8.3f%6.2f%6.2f\n",$1,$2,$3,$4,$5,$6,$7,$8,$9,$10 ;
       }
    else if(length($3)==1){
       printf "%s   %4d  %-1s   %s   %s%12.3f%8.3f%8.3f%6.2f%6.2f\n",$1,$2,$3,$4,$5,$6,$7,$8,$9,$10 ;
       }
    }
 else {
    print $0;
    }
}

finding max and min in a file

awk 'NR == 1 {max=$1 ; min=$1} $1 >= max {max = $1} $1 <= min {min = $1} END { print min , max }'  file.out > minmax.out

finding mean and standard deviation

awk '{array[NR]=$1; sum+=$1}  END {for (m=1;m<=NR; m++) { sumsq+= (array[m]-(sum/NR))**2}  print sum/NR , sqrt(sumsq/NR) }' DATA_FILE  >MEAN_SD.out

reading file twice

simply, you have type filename twice.
The value of FNR starts from 1, increases until the end of the current file, starts again from 1 as soon as the first line of the next file is read, and so on. So, the condition "NR==FNR" is only true while awk is reading the first file.
forexample, replace each number with its difference from the maximum:

awk 'NR==FNR{if($0>max) max=$0;next} {$0=max-$0}1' file file

sign function

awk 'function sgn(x){ if (x>0) return 1; else if (x<0) return -1 ; else return 0} {print $1} data_file > result
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License