# How to Use the Awk “If Else” Statement

> The awk "if-else" statement is used in Linux to process text files line by line in a conditional manner. Here's how to do it.

- Author: [Bhagwad Park](https://www.namehero.com/blog/author/bhagwad-park/)
- Published: 2024-11-27
- Updated: 2024-11-29
- Category: [Linux Command Line](https://www.namehero.com/blog/category/linux-command-line/), [VPS Hosting](https://www.namehero.com/blog/category/vps-hosting/)
- Web page: https://www.namehero.com/blog/how-to-use-the-awk-if-else-statement/

[![](https://blogcdn.namehero.com/blog/wp-content/uploads/2024/11/29085701/howtouseawk-1024x576.png)](https://blogcdn.namehero.com/blog/wp-content/uploads/2024/11/29085701/howtouseawk.png)

Earlier, I’d written about how the [awk statement in Linux](https://www.namehero.com/blog/awk-command-everything-you-need-to-know/) 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

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](https://blogcdn.namehero.com/blog/wp-content/uploads/2024/11/25121531/Awk-If-Else-Demonstration.png)](https://blogcdn.namehero.com/blog/wp-content/uploads/2024/11/25121531/Awk-If-Else-Demonstration.png)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](https://blogcdn.namehero.com/blog/wp-content/uploads/2024/11/25122746/Multiple-Line-Awk-Commands.png)](https://blogcdn.namehero.com/blog/wp-content/uploads/2024/11/25122746/Multiple-Line-Awk-Commands.png)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.

## Related posts

- [Curl Headers, Cookies, and Authentication](https://www.namehero.com/blog/curl-headers-cookies-and-authentication/)
- [Linux Process Management: ps, htop, kill, and nice](https://www.namehero.com/blog/linux-process-management-ps-htop-kill-and-nice/)
- [Linux find with exec: + vs ;](https://www.namehero.com/blog/linux-find-with-exec-vs/)
