Reference
Synopsis
awk '$1 > $2 {print $1,$2,$1-$2}' filename
The part outside the curly braces is called the "pattern", and the part inside is the "action". The comparison operators include the ones from C:
== != < > <= >= ?:
If no pattern is given, then the action applies to all lines.
Usage
-
pass shell variables to awk
awk -v var=$var 'pattern {program}'
Examples
-
join every two lines in a text file
awk '{ if ( ( NR % 2 ) == 0 ) { printf("%s\n",$0) } else { printf("%s ",$0) } }' file
-
other
awk -F "\t" '$3 > 18 {print $1}' all_mat.glue awk -F " " '{ print $2"\t"$3"\t"set $3+25 }' temp2/$i > $i.coordinates
-
ENSEMBL to UCSC gene annotation. (chromosome name conversion, strand information conversion)
grep -vP '\t\t' $i | cut -f 1-6 | awk '{ if ($1!="Chromosome") { if ($6=="1") {printf("chr%s\t+\n",$0)} else {printf("chr%s\t-\n",$0)} } }' | cut -f 1-5,7 | sed 's/chrMT/chrM/' > $out
-
misc
grep -P 'final|total|Index' 0513.log | tr '=' ' ' | awk '{ if (/:/) {print $NF} else { print $1 }}'| awk '{ if (NR%5==0) {printf("%s\n",$1)} else {printf("%s ",$1)} }'
Hide Comments