GIT –  is oriented toward delimited fields on a per-line basis. It has much more robust programming constructs including if/elsewhiledo/while and for (C-style and array iteration). There is complete support for variables and single-dimension associative arrays plus (IMO) kludgey multi-dimension arrays

We will obtain information from a file that contains the data listed below:

Name LastN State salary bonus age
George Mas Indiana 15000 10000 21
Mike Lol NewYork 25000 2000 28
Jim Fluf Florida 35000 30000 30
Frank Tex Texas 45000 4000 40
Martin Gus  Ohio 5000 1200 25
Bob Gadget California 6000 2000 45
Vlad Dug NewYork 50000 50000 35

Conditional search and math operator example:

  • Display records that have a salary of more than 3000 and a bonus of more than 1000 and is from NY. Display the total of salary and bonus for each those records.

awk ‘$4>3000 && $5>1000 && $3=/NewYork/ {print $4+$5}’ datafile

Output:

27000
100000

  • Display records that have a salary of more than 3000 and a bonus of more than 1000 and is from NY. Display the total of salary and bonus for each those records. Also Display the names and States which those records belong too.

awk ‘$4>3000 && $5>1000 && $3=/NewYork/ {print ” Total Salary for ” $1 ” ” $2 ” is ” $4+$5}’ datafile

Output:

Total Salary for mike Lol is 27000
Total Salary for Vlad Kooch is 100000

awk ‘$4>3000 && $5>1000  {print ” Total Salary for ” $1 ” ” $2 ” is ” ($4+$5)  ” Place of Residence is:  ”  $3   }’ datafile2 | grep ‘NewYork’

Output:

Total Salary for mike lol is 27000 Place of Residence is: NewYork
Total Salary for Vlad Kooch is 100000 Place of Residence is: NewYork

  • The printf command

awk2

 

awk if statements

  • By using the if statement display records that have a salary of more than 3000 and a bonus of more than 1000 and is from NY. Display the total of salary and bonus for each those records.

awk ‘{if ($4>3000 && $5>1000 && $3=/NewYork/) print “The salary for employees working in $3 is as follows:”  $4+$5; else “Nothing here”}’ datafile

Print Friendly

Comments

comments

Bài viết liên quan