Wednesday, February 3, 2010

7 Powerful Awk Operators Examples (Unary, Binary, Arithmetic, String, Assignment, Conditional, Reg-Ex Awk Operators)

This article is part of the on-going Awk Tutorial Examples series. In our earlier awk articles, we discussed about awk printawk user-defined variables, and awk built-in variables.
Like any other programming language Awk also has lot of operators for number and string operations. In this article let us discuss about all the key awk operators.

There are two types of operators in Awk.
  1. Unary Operator – Operator which accepts single operand is called unary operator.
  2. Binary Operator – Operator which accepts more than one operand is called binary operator.

Awk Unary Operator

OperatorDescription
+Positivate the number
-Negate the number
++AutoIncrement
AutoDecrement

Awk Binary Operator

There are different kinds of binary operators are available in Awk. It is been classified based on its usage.

Awk Arithmetic Opertors

The following operators are used for performing arithmetic calculations.
OperatorDescription
+Addition
-Subtraction
*Multiplication
/Division
%Modulo Division

Awk String Operator

For string concatenation Awk has the following operators.
OperatorDescription
(space)String Concatenation

Awk Assignment Operators

Awk has Assignment operator and Shortcut assignment operator as listed below.
OperatorDescription
=Assignment
+=Shortcut addition assignment
-=Shortcut subtraction assignment
*=Shortcut multiplication assignment
/=Shortcut division assignment
%=Shortcut modulo division assignment

Awk Conditional Operators

Awk has the following list of conditional operators which can be used with control structures and looping statement which will be covered in the coming article.
OperatorDescription
>Is greater than
>=Is greater than or equal to
<Is less than
<=Is less than or equal to
<=Is less than or equal to
==Is equal to
!=Is not equal to
&&Both the conditional expression should be true
||Any one of the conditional expression should be true

Awk Regular Expression Operator

OperatorDescription
~Match operator
!~No Match operator

Awk Operator Examples

Now let us review some examples that uses awk operators. Let us use /etc/passwd as input file in these examples.
$ cat /etc/passwd
gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/bin/sh
libuuid:x:100:101::/var/lib/libuuid:/bin/sh
syslog:x:101:102::/home/syslog:/bin/false
hplip:x:103:7:HPLIP system user,,,:/var/run/hplip:/bin/false
saned:x:110:116::/home/saned:/bin/false
pulse:x:111:117:PulseAudio daemon,,,:/var/run/pulse:/bin/false
gdm:x:112:119:Gnome Display Manager:/var/lib/gdm:/bin/false

Awk Example 1: Count the total number of fields in a file.

The below awk script, matches all the lines and keeps adding the number of fields in each line,using shortcut addition assignment operator. The number of fields seen so far is kept in a variable named ‘total’. Once the input has been processed, special pattern ‘END {…}’ is executed, which prints the total number of fields.
$ awk -F ':' '{ total += NF }; END { print total }' /etc/passwd
49

Awk Example 2: Count number of users who is using /bin/sh shell

In the below awk script, it matches last field of all lines containing the pattern /bin/sh. Regular expression should be closed between //. So all the frontslash(/) has to be escaped in the regular expression. When a line matches variable ‘n’ gets incremented by one. Printed the value of the ‘n’ in the END section.
$ awk -F ':' '$NF ~ /\/bin\/sh/ { n++ }; END { print n }' /etc/passwd
2

Awk Example 3: Find the user details who is having the highest USER ID

The below awk script, keeps track of the largest number in the field in variable ‘maxuid’ and the corresponding line will be stored in variable ‘maxline’. Once it has looped over all lines, it prints them out.
$ awk -F ':'  '$3 > maxuid { maxuid=$3; maxline=$0 }; END { print maxuid, maxline }' /etc/passwd
112 gdm:x:112:119:Gnome Display Manager:/var/lib/gdm:/bin/false

Awk Example 4: Print the even-numbered lines

The below awk script, processes each line and checks NR % 2 ==0 i.e if NR is multiples of 2. It performs the default operation which printing the whole line.
$ awk 'NR % 2 == 0' /etc/passwd
libuuid:x:100:101::/var/lib/libuuid:/bin/sh
hplip:x:103:7:HPLIP system user,,,:/var/run/hplip:/bin/false
pulse:x:111:117:PulseAudio daemon,,,:/var/run/pulse:/bin/false

Awk Example 5.Print every line which has the same USER ID and GROUP ID

The below awk script prints the line only if $3(USER ID) an $4(GROUP ID) are equal. It checks this condition for each line of input, if it matches, prints the whole line.
$awk -F ':' '$3==$4' passwd.txt
gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/bin/sh

Awk Example 6: Print user details who has USER ID greater than or equal to 100 and who has to use /bin/sh

In the below Awk statement, there are two conditional expression one is User id($3) greater than or equal to 100, and second is last field should match with the /bin/sh , ‘&&’ is to print only if both the above conditions are true.
$ awk -F ':' '$3>=100 && $NF ~ /\/bin\/sh/' passwd.txt
libuuid:x:100:101::/var/lib/libuuid:/bin/sh

Awk Example 7: Print user details who doesn’t have the comments in /etc/passwd file

The below Awk script, reads each line and checks for fifth field is empty, if it is empty, it prints the line.
$awk -F ':' '$5 == "" ' passwd.txt
libuuid:x:100:101::/var/lib/libuuid:/bin/sh
syslog:x:101:102::/home/syslog:/bin/false
saned:x:110:116::/home/saned:/bin/false

1 comment:

  1. Great sharing. Very informative. Thanks a lot for sharing!!!

    Regards,
    clipping path service

    ReplyDelete