Saturday, 21 November 2015

Logical Pattern Matching in AWK

Input File:F_Data_File.txt

EMPID|ENAME|DEPTNO|ACTV_FLG|SALARY
1001|A1|10|Y|10000
1002|A2|20|Y|20000
1003|A3|30|N|30000
1004|A4|10|Y|40000
1005|A5|10|N|10000
1006|A6|20|N|30000
1007|A7|20|N|50000
1008|A8|30|N|10000

List out all the Employees who belongs to department 10

$ awk -F'|' '$3=="10"{print $0}' F_Data_File.txt

Output:
1001|A1|10|Y|10000
1004|A4|10|Y|40000
1005|A5|10|N|10000

List out all the Employees who belongs to department 10 and Active

$ awk -F'|' '$3=="10" && $4=="Y" {print $0}' F_Data_File.txt

Output:
1001|A1|10|Y|10000
1004|A4|10|Y|40000

List out all the Employees who belongs to department 10 or 30

$ awk -F'|' '$3=="10" || $3=="30" {print $0}' F_Data_File.txt

Output:
1001|A1|10|Y|10000
1003|A3|30|N|30000
1004|A4|10|Y|40000
1005|A5|10|N|10000
1008|A8|30|N|10000

List out all the Employees who belongs to department 10 or 30 and are Active

$ awk -F'|' '($3=="10" || $3=="30") && $4=="Y" {print $0}'
  F_Data_File.txt

Output:
1001|A1|10|Y|10000
1004|A4|10|Y|40000

List out all the Employees who salary is greater than 25000

$ awk -F'|' '$5>"25000" {print $0}' F_Data_File.txt

Output:
1003|A3|30|N|30000
1004|A4|10|Y|40000
1006|A6|20|N|30000
1007|A7|20|N|50000

For pattern matching follow article Basic Pattern Matching in AWK

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...