≡ Menu

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

Linux Awk Tutorials - Awk Reg-Ex Operator ExamplesThis article is part of the on-going Awk Tutorial Examples series. In our earlier awk articles, we discussed about awk print, awk 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

Operator Description
+ Positivate the number
Negate the number
++ AutoIncrement

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.

Operator Description
+ Addition
* Multiplication
/ Division
% Modulo Division

Awk String Operator

For string concatenation Awk has the following operators.

Operator Description
(space) String Concatenation

Awk Assignment Operators

Awk has Assignment operator and Shortcut assignment operator as listed below.

Operator Description
= 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.

Operator Description
> 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

Operator Description
~ 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
hplip:x:103:7:HPLIP system user,,,:/var/run/hplip:/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

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

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

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

Recommended Reading

Sed and Awk 101 Hacks, by Ramesh Natarajan. I spend several hours a day on UNIX / Linux environment dealing with text files (data, config, and log files). I use Sed and Awk for all my my text manipulation work. Based on my Sed and Awk experience, I’ve written Sed and Awk 101 Hacks eBook that contains 101 practical examples on various advanced features of Sed and Awk that will enhance your UNIX / Linux life. Even if you’ve been using Sed and Awk for several years and have not read this book, please do yourself a favor and read this book. You’ll be amazed with the capabilities of Sed and Awk utilities.

Add your comment

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Comments on this entry are closed.

  • frankhuang November 18, 2010, 11:54 pm

    good, $NF can also be replaced by $0

  • hamsolo474 October 20, 2011, 4:06 am

    in awk example 5
    $awk -F ‘:’ ‘$3==$4’ passwd.txt
    gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/bin/sh

    i cant get my line to work
    awk -F ‘”,”|”‘ “$1 ==1{print $6;}” joblist2.csv
    im using awk to reference fields in a CSV, i have numbered all the rows and thats what the regular expression is meant to find
    i cant for the life of me figure out why it doesnt work