Docsity

Awk Syntax and Functionality: Conditions, Variables, Arrays, and Actions, Assignments of Computer Science

Toggle sidebar
bg1
AWK Syntex
awk -Fc -f awk.code InputFile
awk -Fc 'condition action' InputFile
e.g. awk -F: '/cscqxcx/ {print $1," uses ",$7}' /etc/passwd
Condition
1) Regular expression
2) pattern-Matching expressions
$n ~ /re/
$n !~ /re/
e.g. awk -F: '$7 ~ /bash/ {print $1}' /etc/passwd | sort
3) Relational Character Expression (==, !=, <, <=, >, >=)
$n == "s"
$n != "s"
e.g. awk -F: '$7 != "/bash/" {print $1}' /etc/passwd | sort
4) Relational Number Expressions (==, !=, <, <=, >, >=)
5) Mixing and conversion of numbers and characters
e.g. awk ‘BEGIN {one=1;two=2; print (one two) +3}’
6) Ranges
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
7) BEGIN, END
Variables
1) Assignment operators (=, +=, -=, *=, /=, ^=)
x=y; x+=y (x=x+y); x-=y (x=x-y); x*=y (x=x*y); x/=y (x=x/y); x^y (x=x^y)
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Awk Syntax and Functionality: Conditions, Variables, Arrays, and Actions and more Assignments Computer Science in PDF only on Docsity!

AWK Syntex

awk -Fc -f awk.code InputFile

awk -Fc 'condition action' InputFile

e.g. awk -F: '/cscqxcx/ {print $1," uses ",$7}' /etc/passwd

ƒ Condition

  1. Regular expression

  2. pattern-Matching expressions

$n ~ /re/

$n !~ /re/

e.g. awk -F: '$7 ~ /bash/ {print $1}' /etc/passwd | sort

  1. Relational Character Expression (==, !=, <, <=, >, >=)

$n == "s"

$n != "s"

e.g. awk -F: '$7 != "/bash/" {print $1}' /etc/passwd | sort

  1. Relational Number Expressions (==, !=, <, <=, >, >=)

  2. Mixing and conversion of numbers and characters

e.g. awk ‘BEGIN {one=1;two=2; print (one two) +3}’

  1. Ranges

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

  1. BEGIN, END

ƒ Variables

  1. Assignment operators (=, +=, -=, =, /=, ^=) x=y; x+=y (x=x+y); x-=y (x=x-y); x=y (x=x*y); x/=y (x=x/y); x^y (x=x^y)
  1. Increment and decrement
    x++ ({x=1; print x++;} -->1); ++x ({x=1; print ++x;} -->2); x-- ({x=1; print x--;} -->1); --x ({x=1; print --x;} -->0); e.g. awk ‘BEGIN {x=1;y=1; print “x=”x, “y=”y; print “x++=”x++, “++y=”++y, “x=”x, “y=”y}’
  2. Build-in variables
  • Positional variables ($0, $1, $2,….)
  • Field separator and Co FS (Field separator); RS (Record separator, by default “\n”); OFS (output field separator); ORS (output record separator); FIELDWIDTHS , a space-separated list of field widths to use for splitting up the record. If it is put in a BEGIN block, it will override the use of FS. e.g. awk ‘BEGIN {FIELDWIDTHS=”3 4 1”} {print $0”\n”, $1, $2, $3, $4, $NF}’ /etc/passwd
  • File and line information NF (Number of fields); NR (Number of record); FILENAME; FNR(File number of records)
  • Command line parameters Variable ARGV is in fact an array. ARGV[0], ARGV[1] ARGC (the number of used ARGV items variables
  • Shell environment Variable ENVIRON is in fact an array. e.g. awk ‘BEGIN {print ENVIRON[“HOME”]}’
  1. Arrays (associative arrays or hashes)
  • Assigning array elements array[index] = value e.g. awk ‘BEGIN {data[1]=123;data[2]=”junk”; data[“input”]=23.45; print data[1], data[2], data[“input”]}’

Split(string, array, fieldseparator)

  • Referring to an array element array[index] Or to use the expression index in array
  • Scanning an array for (variable in array) commands e.g. awk ‘BEGIN {data[1]=123;data[2]=”junk”; data[“input”]=23.45; for (x in data) print “index=”x, “value=”data[x];}’
  • Deleting an array or element delete array delete array[index]

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

  • Leaving loops while (condition){ Command_l1; Command_l2; break ; >---- Command_l3; } | Command Å

while (condition){ Å Command-l1; | Command-l2; | continue ; >---- Command-l3; }

exit n

ƒ Actions

  1. Printing print item1, item2,…. print “text: “ variable

\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

  1. 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])

  2. 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