16.05.2015 Views

Working with the Unix OS

Working with the Unix OS

Working with the Unix OS

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Unix</strong> Tools<br />

# print last input line<br />

{ last = $0 }<br />

END { print last }<br />

# counting lines, words and characters<br />

{ nc = nc + length($0) + 1<br />

nw = nw + NF<br />

}<br />

END { print NR, "lines,", nw, "words,", nc, "characters" }<br />

! Flow Control<br />

# compute total & average pay of employees above $6/hour<br />

$2 > 6 { n = n + 1; pay = pay + $2 * $3 }<br />

END {<br />

if (n > 0)<br />

print n, "employees, total pay is", pay, "average is", pay/n<br />

else<br />

print "no employees are paid more than $6/hour"<br />

}<br />

# interest1 – compute compound interest<br />

# input: amount rate years<br />

# output: compounded value at end of each year<br />

{ i = 1<br />

while (i 4<br />

# Print all input lines where last field is greater than 4<br />

$NF > 4<br />

# Print <strong>the</strong> total number of fields in all input lines<br />

{ nf = nf + NF }<br />

END {print nf}<br />

# Print <strong>the</strong> total number of lines that contain Mark<br />

/Mark/ { nlines = nlines + 1 }<br />

END {print nlines}<br />

# Print <strong>the</strong> largest field first and line that contains it<br />

$1 > max { max = $1; maxline = $0 }

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!