





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






awk -Fc -f awk.code InputFile
awk -Fc 'condition action' InputFile
e.g. awk -F: '/cscqxcx/ {print $1," uses ",$7}' /etc/passwd
Regular expression
pattern-Matching expressions
$n ~ /re/
$n !~ /re/
e.g. awk -F: '$7 ~ /bash/ {print $1}' /etc/passwd | sort
$n == "s"
$n != "s"
e.g. awk -F: '$7 != "/bash/" {print $1}' /etc/passwd | sort
Relational Number Expressions (==, !=, <, <=, >, >=)
Mixing and conversion of numbers and characters
e.g. awk ‘BEGIN {one=1;two=2; print (one two) +3}’
A range of records can be specified using two patterns separated by a comma.
e.g. awk –F: '/cscqxcx/, /cscabrx/ {print $1 “ “ $3 }' /etc/passwd
e.g. awk -F: '/cscqxcx/, /cscqxcx/ {print $1 “ “ $3 }' /etc/passwd
Variables
Split(string, array, fieldseparator)
Command(s)} }
e.g. awk 'BEGIN {ORS=" "; {for (i=1;i<=10; i++){ print i^2}}; print "\n"}'
1 4 9 16 25 36 49 64 81 100
while (condition){ Å Command-l1; | Command-l2; | continue ; >---- Command-l3; }
exit n
Actions
\a alert (system bell) \t a horizontal tabulator is printed \v a vertical tabulator is printed \n when this character is printed a new line will be started. \b backspace character
printf(format, item1, item2,…)
spec. value result comment %s 10.63 10.63 print a string
%i 10.63 10 print only the integer part of the number %e 10.63 1.063000e+01 print a number in scientific notation %f 10.63 10.6300000 print a number either in floating point %g 10.63 10.63 print the less characters in float or scientific format .n precision
Numerical calculations int(x) log(x) exp(x) sqrt(x) sin(x) atan2(y,x) arctangent of y/x in radians cos(x) rand() e.g. int(n*rand()) srand([x])
String manipulation substr(target, start, length) e.g. awk ‘ BEGIN {target=”ComputerScience”; print substr(target, 7, 3); print(target)}’
global substitution: gsub(regex, substitute, target)
sub(regex, substitute, target) e.g. awk ‘BEGIN {target=”ComputerScience”; sub(/er/, “er “, target); print(target)}’
general substitution:
gensub(regex, substitute, how, target)
index(target, find)
e.g. awk ‘BEGIN {target=”atgctag”; print index(target, “gct”)}’
length(target)
match(target, regex, array)
{print $1, $3, NF, $NF}
END {print "End of file"}
$ awk -f awk2 float
$ cat awk2.
BEGIN {print "start of file:", FILENAME}
{print $1 $3 NF $NF}
END {print "End of file"}
$ awk -f awk2.1 float
$ cat awk2.
BEGIN {print "start of file:", FILENAME}
{print $1 " " $3 " " NF " " $NF}
END {print "End of file"}
$ awk -f awk2.2 float
$ cat awk
NR>1 && NR<4 { print NR, $1, $3, $NF}
$ awk -f awk3 float
$ cat float
Wish:I:was:floating:in:blue:across:the:sky,
My:imagination:is:strong,
And:I:often:visit:the:days
When:everything:seemed:so:clear
Now:I:wonder:what:I'm:doing:here:at:all...
$ awk -F: -f awk3 float
$ cat awk
BEGIN {print "Scanning file ...."}
{
printf "line %d: %s\n", NR, $0;
lineCount++;
wordCount+=NF;
}
END {printf "lines = %d, workds = %d\n", lineCount, wordCount}
$ awk -f awk4 float
$ cat awk
{
for (i = NF; i>= 1; i--)
printf "%s ", $i;
printf "\n";
}
$ awk -f awk5 float