• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
NameHero® Blog

NameHero® Blog

Web Hosting Tips & Resources From NameHero

  • Hosting
    • Web Hosting
    • WordPress Hosting
    • WooCommerce Hosting
    • Enterprise Hosting
  • VPS
    • VPS Hosting
    • Flex VPS
  • Reseller
  • Email
  • Gaming
  • Domains
  • Website Builder
  • Account
  • Blog Home
  • Categories
  • Authors

How to Use the Awk “If Else” Statement

Bhagwad Park

Published on: November 27, 2024

Categories: Linux Command Line, VPS Hosting 0

Earlier, I’d written about how the awk statement in Linux can be a superior alternative to grep in many situations. One of those included the ability to perform complex operations, including control loops. The “if-else” statement is one of the most basic control loops in all languages. While it’s strange to view awk as a programming language, it does nonetheless possess many characteristics of a programming language such as control structures, variables, and functions. Granted, it’s a highly domain-specific language, but it is one, nonetheless.

Let’s look at the basic syntax, ways to make the awk command more readable, and some use-case scenarios where we can find the awk if-else loop useful. The scenarios below are common enough that they can be used by anyone.

  • Basic Usage of awk if-else
  • Making Awk Control Loops Easier to Read
  • Use Case Scenarios for Awk If-Else Statements
  • Categorizing Data
  • Filtering Information
  • Modify Values Based on a Condition
  • Marking Down Data Conditionally
  • Conclusion

Basic Usage of awk if-else

Here’s what the basic usage of the awk if-else control loop looks like:

awk '{ if (condition) { action1 } else { action2 } }' file

Those of you familiar with programming languages, will see that it’s very similar to a language like C, and this isn’t a coincidence since awk was developed in the 1970s, and one of the authors was none other than Brian Kernighan himself – the co-creator of C and co-author of one of its defining books – “The C Programming Language”. Given this background, it’s no surprise that awk shares its syntax with C.

Here’s a sample awk if-else command that illustrates how the arguments work:

awk '{ if ($2 < 25) { print $1, "Young" } else { print $1, "Adult" } }' data.txt

The above command takes an input file “data.txt” and parses it line by line. In each line, it examines the second field ($2) and if it’s less than 25, it prints the first field followed by the word “Young” otherwise it prints “Adult”. Here are the contents of “data.txt”:

Alice 30
Bob 25
Charlie 20

And this is what the output looks like:

Awk If-Else Demonstration
Awk If-Else Demonstration

As you can see, only Charlie on the third line has a number next to it that’s less than 25, and so the awk command prints “Young” next to his name, and “Adult” for the others. This is a simple example of an awk conditional if-else statement.

Making Awk Control Loops Easier to Read

If you’re using awk on a command line, you might find the command difficult to read because it’s all on the same line. Particularly if you have opening and closing curly brackets with different code blocks, it can be confusing to keep track of them all, and debugging is difficult. To avoid this, I suggest you use a single apostrophe on the first line as a kind of code block that allows you to type multiple lines before the final apostrophe like this:

awk '{
if ($2 < 25) {
print $1 " is young";
} else {
print $1 " is an adult";
}
}' data.txt

In the above code, the entire pattern matching and action code is surrounded by an apostrophe (‘), which allows you to split it into multiple lines. Here’s a screenshot:

Multiple Line Awk Commands
Multiple Line Awk Commands

The above code is easier to read, not just because of the multiple lines, but because of the indentation. In the command line, you can also force new lines by using a backslash (\), but I prefer the apostrophe method because it’s neater. With this kind of structure, you can easily see what’s wrong with a command if you forget a curly bracket somewhere.

Since you need to encapsulate the code for awk within single quotes anyway, it’s very convenient for splitting into multiple lines.

Use Case Scenarios for Awk If-Else Statements

Here are some cool scenarios for which you can use the if-else statement in awk. Note that for all of these, something like grep would be woefully under-equipped.

Categorizing Data

Let’s say you have a file with a list of students and their grades, separated by a delimiter like a space. Using awk, you can quickly go line by line and determine if they’ve passed or failed using a statement like this:

awk '{ if ($2 >= 50) print $1 " Pass"; else print $1 " Fail"; }' scores.txt

In the above code, if the grade is greater than or equal to “50”, awk prints the name of the student with “Pass” next to it, otherwise it says “fail”. We’ve already seen an example of this earlier when we were classifying people into adults. This code can be used for any text file where we have to decide on records one at a time.

Filtering Information

Related to the previous use case, we can use awk to filter lines that meet specific criteria. For example, if I have a text file with thousands of records of people and their ages, and I want to only list the adults, I can use the following awk if-else statement:

awk '{ if ($2 > 30) print $0; }' data.txt

The above code will only print those lines where the age of the person is greater than 30. While this doesn’t have an “else” statement, it illustrates the various scenarios where we can use awk. This is particularly useful for large text files, where you can’t manually scan everything.

Modify Values Based on a Condition

Let’s say we have a text file of names with salaries next to them. We want to calculate a year-end bonus based on the following, simple rule: If someone has a salary less than $3000 a month, then give them an additional bonus of 10% and print the total value of the compensation that they will receive. Here’s the code to accomplish that:

awk '{ if ($2 < 3000) print $1, $2 * 1.1; else print $1, $2; }' salaries.txt

In this, awk tests to see if the person is earning less than 3000, and if they do, then it prints the second column after multiplying it by 1.1. If the condition isn’t met, then awk just prints the salary as is. While it’s an unrealistic scenario (for those who earn just above $3000), you can make the calculation more realistic if you want!

Marking Down Data Conditionally

Let’s say you have a file with some data, and you want to mark up some data conditionally so that you can pass it on to someone else who will be able to see the annotations. In this scenario, you don’t want to remove data or modify it, but simply add some existing annotation. Here’s an example:

awk '{ if ($3 > 100) print $0 " [High Temperature]"; else print $0; }' weather.txt

In this scientific data example, the awk script marks the data with “High Temperature” if the number exceeds 100 degrees. The temperature is in the third column, so we use $3. In both cases, it prints the whole line as well. You can also use the above code to search for complex temperature patterns and annotate them accordingly. For example, you can compare the highest and the lowest temperatures in different fields and based on some criteria, you can draw attention to specific records.

Conclusion

As you can see, the awk “if-else” statement is quite useful for less unusual scenarios. It’s great for working with large text files, where each line contains a single record. While there are some similarities between awk and grep, the latter can’t perform most of the complex operations of which awk is capable. Awk’s programming characteristics give it a flexibility that grip simply can’t match.

Bhagwad Park Profile Picture
Bhagwad Park

I’m a NameHero team member, and an expert on WordPress and web hosting. I’ve been in this industry since 2008. I’ve also developed apps on Android and have written extensive tutorials on managing Linux servers. You can contact me on my website WP-Tweaks.com!

Related Posts

Grub Rescue: Everything You Need To Know

Grub rescue is the Linux screen you hope you never see. Here is what it is, and what can cause it. It's best to be prepared!

How to Use the xxd Command in Linux

We use the xxd command in Linux to look at the hexadecimal representation of files. While this might seem odd, it's actually super useful!

What Is /dev/shm And Why Is It So Useful?

The speed of /dev/shm makes it viable for a variety of use case but first, let's see how it works in its raw form.

How to Use the xargs Command in Linux

The "xargs" command converts text from standard input into arguments, allowing the output for one command to be used as arguments in other.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow & Subscribe

Exclusive promos, content and more!


Most Popular Posts

NameHero’s Recommended WordPress Plugin and Theme Setup (2024)

WordPress Hosting vs. Web Hosting – What’s The Difference?

How To Increase The InnoDB Buffer Pool Size

How To Fix A Stuck All-in-One WP Migration Import

How To Add A Subdomain In Cloudflare

Top Categories

  • WordPress
  • WordPress Tutorials
  • Enterprise Hosting
  • WooCommerce
  • Web Hosting
  • Resellers
  • Website Security
  • Website Development
  • Website Performance
  • VPS Hosting
  • SEO Tips
  • Announcements
  • Domain Registration
NameHero

NameHero® proudly provides web hosting to over 40,000 customers with 99.9% uptime to over 750,000 websites.

  • Master Card
  • Visa
  • American Express
  • Discover
  • Paypal
Products
  • Web Hosting
  • VPS Hosting
  • Flex VPS Hosting
  • WordPress Hosting
  • WooCommerce Hosting
  • Reseller Hosting
  • Enterprise Hosting
  • Email Hosting
  • Game Hosting
  • Domains
  • Website Builder
Help & Support
  • NameHero Blog
  • NameHero Gaming Blog
  • Support
  • Help Center
  • Migrations
  • Affiliates
  • Gaming Affiliates
  • Call 1-855-984-6263
Company
  • About Us
  • Contact Sales
  • Reviews
  • Uptime
  • We're Hiring

Copyright © 2025 Name Hero, LLC. All rights reserved.
NameHero® is a registered trademark.

  • Privacy Policy
  • Terms of Use
  • Acceptable Use Policy
  • Payment Policy
  • DMCA