Conditional Statements in Awk

Conditional statements are one of the most powerful features of awk, allowing users to execute different commands based on the values of variables or expressions. This enables the creation of complex logic within awk scripts, and facilitates the performance of advanced text processing tasks.

In this tutorial, we will cover the basics of conditional statements in awk, including the syntax and usage of the ‘if-else’ statement. For example:

  1. If-else statements with awk
  2. Switch Statement with Awk
  3. Ternary Operators with Awk
  4. If-else Statement with Awk

    The ‘if-else’ statement is a basic conditional statement in awk. It allows you to specify different commands to execute based on a particular condition. The syntax for the ‘if-else’ statement is as follows:

    if (condition) {
       # commands to execute if condition is true
    }
    else {
       # commands to execute if condition is false
    }

    Here, ‘condition’ is a boolean expression that evaluates to either true or false. If the condition is true, the commands inside the curly braces following ‘if’ are executed. If the condition is false, the commands inside the curly braces following ‘else’ are executed.

    For example, let’s say we have a file containing a list of numbers, and we want to print the numbers greater than 10. We can use the ‘if-else’ statement to accomplish this task as follows:

    awk '{ if ($1 > 10) { print $1 } }' numbers.txt

    Here, the ‘if’ statement checks whether the first field ($1) is greater than 10. If it is, the number is printed. If it isn’t, nothing happens.

    You can also use the ‘if-else’ statement to perform more complex tasks. For example, let’s say we have a file containing a list of user names and their ages, and we want to print the user names of users who are over 18 years old. We can use the ‘if-else’ statement to accomplish this task as follows:

    awk '{ if ($2 > 18) { print $1 } else { print "User is not old enough" } }' users.txt

    Here, the ‘if’ statement checks whether the second field ($2) is greater than 18. If it is, the user name is printed. If it isn’t, the string “User is not old enough” is printed.

    Switch Statement with Awk

    The ‘switch’ statement in awk allows you to execute different commands based on multiple conditions. It provides a more concise way of expressing multiple ‘if-else’ statements. The syntax for the ‘switch’ statement is as follows:

    switch (variable) {
       case value1: 
          # commands to execute if variable == value1
          break
       case value2:
          # commands to execute if variable == value2
          break
       ...
       default:
          # commands to execute if none of the cases match
    }

    Here, ‘variable’ is the variable or expression that you want to compare against different values. ‘value1’, ‘value2’, etc. are the different values that you want to compare against. The ‘break’ statement is used to exit the ‘switch’ statement after a match is found. The ‘default’ keyword is used to specify commands to execute if none of the cases match.

    For example, let’s say we have a file containing a list of programming languages, and we want to print the name of the programming language based on the file extension. We can use the ‘switch’ statement to accomplish this task as follows:

    awk '{ switch ($NF) { case "py": print "Python"; break; case "rb": print "Ruby"; break; case "js": print "JavaScript"; break; default: print "Unknown" } }' languages.txt

    Here, we use the ‘switch’ statement to compare the last field ($NF) against different values (‘py’, ‘rb’, ‘js’). If the value matches, we print the name of the programming language. If it doesn’t match, we print “Unknown”.

    Ternary Operators with Awk

    The ‘ternary operator’ in awk is a shorthand version of the ‘if-else’ statement. It allows you to perform a simple if-else operation in a single line. The syntax for the ternary operator is as follows:

    (condition) ? true-value : false-value

    Here, ‘condition‘ is the boolean expression that you want to evaluate. ‘true-value’ is the value to return if the condition is true. ‘false-value’ is the value to return if the condition is false.

    For example, let’s say we have a file containing a list of numbers, and we want to print “Even” if the number is even, and “Odd” if the number is odd. We can use the ternary operator to accomplish this task as follows:

    awk '{ print ($1 % 2 == 0) ? "Even" : "Odd" }' numbers.txt

    Here, we use the ternary operator to check whether the number in the first field ($1) is even or odd. If it is even, we print “Even”. If it is odd, we print “Odd”.

    Conclusion

    In conclusion, conditional statements are a powerful feature of awk that allow users to perform advanced text processing tasks. The ‘if-else’ statement is a basic conditional statement that enables you to execute different commands based on a particular condition. By mastering the ‘if-else’ statement and other conditional statements, you can become more effective and efficient in your awk scripting, and accomplish complex text processing tasks with ease.

    The post Conditional Statements in Awk appeared first on TecAdmin.

awk General Articles if-else switch TecAdmin ternary operator