4.Brief commands overview
The language looks a little like C but automatically handles input, field splitting, initialization, and memory management.
- Built-in string and number data types
- No variable type declarations
data[1]= 330
data[3]= "text" # yes, the elements could be of different type
data[7]= 7.5
data["Monday"]= 1
awk is a great prototyping language
- start with a few lines and keep adding until it does what you want
awk gets its input from
- files
- redirection and pipes
- directly from standard input
Exercise
- Let's start with something simple Exercises/01.Simple arithmetic.
Common commands and constructions - examples
Loop
for (i=1; i<=10; i++) {
print i
}
Exercise
- Let's try to use loops for some simple tasks Exercises/05.Easy_tricks
If else statement
if ($1 > 2) {
print $2
} else if ($1 < 2) {
print $3
} else {
print $3
}
Exercises
- time to add a bit more, so we can get some meaningful calculations on our data Exercises/Warming up.
While statement
while ($1 < $3) {
getline; print
}
Other
print "Text"$1"more text"
printf ("Text %g more text", $1)
next # (skips the remaining patterns on the current line of input)
exit # (skips the rest of the current line)
Exercises
- Let's see how can we format better the output of an awk script Exercises/String manipulation
- At this point, we should be abe to decipher Multiline Fasta To Single Line Fasta.
Predefined variables
- NR - Number of records processed
- FNR - Number of record processed in the current file while
NR
refers to the total record number. For the first fileFNR==NR
, but for the secondFNR
will restart from 1 whileNR
will continue to increment. - NF - Number of fields in current record
- FILENAME - name of current input file
- FS - Field separator, space or TAB by default
- OFS - Output field separator, space by default
- ARGC/ARGV - Argument Count, Argument Value array - get arguments from the command line
$1 - first field value, $2 - second etc.
$0 - contains the entire line
Built-in functions
Arithmetic
sin(), cos(), atan(), exp(), int(), log(), rand(), sqrt()
String manipulation functions
length(), substitution, find substrings, split strings
Output
print, printf(), print and printf to file
Special
system() - executes a Unix command e.g., system("date") to execute "date" command. Note double quotes around the Unix command
If you have reached this point, perhaps you are already asking where you can read something to begin with. This site is perhaps good for beginners: https://en.wikibooks.org/wiki/AWK