Linux awk if examples

How to use conditional statement in awk command

The conditional statement is used in any programming language to execute any statement based on a particular condition. The conditional statement executes based on the value true or false when if-else and if-elseif statements are used to write the conditional statement in the programming. Awk supports all types of conditional statements like other programming languages. How different conditional statements can be used in awk command is shown in this tutorial.

Syntax:

The syntax for four types of conditional statements is mentioned below.

The statement executes when the if condition returns true.

The statement-1 executes when the if condition is true and the statement-2 executes when if return false.

if ( condition ) {
statement- 1
}
elseif {
statement- 2
}
elseif {
statement- 3
}
…….
else {
statement-n
}

This conditional statement is used for executing a statement based on multiple if condition. If the first condition is false then it checks the second condition. If the second condition is false then it checks the third condition and so on. If all conditions return false then it will execute the statement of else part.

Ternary operator can be used as an alternative of if-else statement. If the condition true the statement-1 will execute and if the condition false then statement-2 will execute.

Example-1: Using simple if in awk

Create a text file named emplyee.txt with the following content. Suppose, you have to find out the department name, age, and salary of the employee whose id is 11002.

employee.txt

A simple if condition is used in the following script to search the id 11002 in the first field of the file. If the condition becomes true then it will print the values of the other fields of the corresponding line otherwise nothing will be printed.

$ awk ‘ <
if($1 ==»11002″)
<
print «Department Name: «,$2,»\n»;
print «Age: «,$3,»\n»;
print «Salary: «,$4,»\n»;

The id, 1102 exists in the file. So, it printed the other values of the employee.

Example-2: Using if-else in awk

Suppose, you want to print the salary information of the employees whose age is less than or equal to 40 and print the age information for other employees. The following awk script can do this task. There is only one employee in employee.txt file whose age is more than 40 and all other employee’s age is less than 40.

Читайте также:  Начальная загрузка системы linux

$ awk ‘ <
if($3 <
print «The salary of «,$1, » is «, $4, «\n»
>
else
<
print «The age of «,$1, » is «, $3, «\n»
>

The following output will appear after running the script.

Example-3: Using if-elseif in awk script

Create a text file named person.txt with the following content.

Create a awk file named if_elseif.awk with the following code to print the favorite color of each person whose name exists in the file. If-elseif statement is used in the script to do this task. The script will read the first field value of the file, employee.txt and check with a particular value. If the first if condition becomes false then it will check the second if condition and so on. When any if condition becomes true then a color value will be assigned. If all conditions become false then None will be assigned as the color value. The favorite color of each person will print or “No person found” will print if no person name matches.

if_elseif.awk

{
name = $1 ;
if ( name == «JACKSON» ) color = «Blue» ;
else if ( name == «MARTIN» ) color = «Black» ;
else if ( name == «LILY» ) color = «Red» ;
else if ( name == «ROBINSON» ) color = «White» ;
else color = «None» ;

if ( color ! = «None» ) print «The favorite color of » , name, «is » , color;
else print «No person found» ;

Run the following command to execute the file if_elseif.awk with person.txt

The following output will appear after running the command.

Example-4: Using ternary operator in awk

The third field of person.txt file contains the profession of each person. The following ternary operator reads each line of the file and matches the third field value with “Manager”. If the value matches then it will print the name of the person and otherwise it will print the gender of the person.

The following output will appear after executing the script. One person with “Manager profession exists in the person.txt. So, the name of one person is printed and gender is printed for other persons.

Example-5: Using multiple conditions in if statement

Logical OR and Logical AND can be used to define multiple conditions in the conditional statement of awk script. The following awk script reads each line of employee.txt and checks the age and designation with particular values. Logical AND is used in the if condition. When the age value is greater than or equal to 30 and designation is “HR” then the corresponding employee id and salary will print.

The following output will appear after executing the script. There is only one employee exists with the designation, “HR” in employee.txt. Id and salary information for this employee is printed here.

Conclusion:

Most common uses of the conditional statement of any standard programming are supported by awk command. How you can use single and multiple conditions in awk is explained by using very simple examples here. Hope after practicing these examples the learner will be able to use conditional statement properly in awk script.

Читайте также:  Linux serial number monitor

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.

Источник

How to use awk if else statement

Awk is a programming language allows for quick manipulation of structured data types and produces formatted output. The name is made by joining first letters of the authors name, Aho, Weinberger, and Kernighan.

The awk command is commonly used to search and manipulate text. During this process, it will search for lines in one or more of the files that contain the given pattern, and then execute the appropriate action.

Awk supports conditional statements to control the flow of the program. The syntax is inspired from C programming language.

This article is going to show you how to use various if-else-elseif statements syntax in AWK.

if statement

The if statement checks the conditions and if they are true, it executes its corresponding action(s).

AWK if example

search in employee.txt contents and print out the department name, age if user whose id is 11002

## employee.txt 11001 Sales 45 $3000 11002 HR 32 $1500 11003 Marketing 26 $1200 11004 HR 25 $2500

if else statement

With the expression if-else , you can specify the list of actions that should be taken if the condition is false. If the condition returns true, statement1 will be executed, and if it returns false, statement2 will be executed.

AWK if-else example

Let’s say you want to print the salary information of the employees under the age of 40 and print the age information for the others. This task can be accomplished using the awk script below.

There is only one employee over the age of 40 in the employee.txt file, all the others are under the age of 40.

if elseif statement

Multiple if conditions are used in this conditional statement to execute a statement. Upon failure of the first condition, it checks the second one. A second false condition leads to a third, then the next. All conditions must return false in order for the else part to be executed.

if (condition) < statement1 >else if(condition) < statement2 >else if(condition) < statement3 >…… else

AWK if-elseif example

Let’s say you want to print the salary information of the employees under the age of 30, then the under-40, then the over-40.

awk ' < if($3 elseif($3 else < print "The salary of ",$1, " is ", $4, "\n" >>' employee.txt

AWK : Ternary operator

You can use ternary operators instead of if-else statements. A true condition would cause statement1 to execute, a false condition would cause statement2 to execute.

(condition) ? statement-1: statement-2

Leave a Comment Cancel reply

Hey! I’m Daan. I work as a SysAdmin in the Netherlands. Whenever I find free time, I write about IT, Linux, Open Source and hardware on this site.

Читайте также:  Установка deb файлов на линукс

VS Code Tutorials

  • Automatic code indentation
  • Comment out multiple lines
  • Quickly duplicate a line in VSCode
  • Create a comment block in VSCode
  • Show hidden files in VSCode
  • Quickly find a file in VSCode
  • How to delete the whole line in VSCode
  • Collapse code blocks in VSCode
  • Enable/disable word wrap in VSCode
  • Bind terminal commands to VSCode keyboard shortcuts
  • VSCode Command Palette
  • VSCode Format On Save
  • VSCode «go to definition» guide

Источник

Awk If, If Else, Else Statement or Conditional Statements

Awk If, If Else, Else Statement or Conditional Statements

Awk is a very popular text processing tool. Awk provides different functionalities and structures like programming languages. if — if else is a very useful conditional statement used to create decision trees. if — if else used to check the given situation or operations and run accordingly. For example, we can check the age of the person and act accordingly if over 60 or below 60.

Example Data

During learning awk if- if else statements we will use following file.

ismail 33 male ahmet 5 male elif 8 female

Awk If Statement

Awk if statement syntax is like below. We will check the condition and if the condition is met or true we will execute the code part. If not we simply skip if code block.

In this example we will look for the name ismail and then print his age.

We will save the awk script as if-program and call with awk -f parameter like below.

Awk If Statement

Or we can provide the script from bash like below.

Awk If Statement

Awk Else If Statement

What will happen if we have more than a single condition to check and execute the script. We can use else if statements for multi-condition situations. Syntax of else if is like below.

if (CONDITION) < CODE >else if (CONDITION) < CODE >. 

We can use more than if else according to our needs. In this example, we will list persons with two different conditions like over 18 and below 18.

When we run this code like below.

$ awk -f else-if-program data.txt

Awk Else If Statement

Awk Else Statement

While using if-else conditional statements we may need to specify default condition where it will meet if none of the other conditions is met. We can it else and put it to the end of the if-else if block. The syntax is like below

if (CONDITION) < CODE >else if (CONDITION) < CODE >. else

In this example we will check the gender and given record is not male we will print a message. In this example, we do not add else-if because we do not need but they can be added without a problem if needed.

We save this script as else-program and run like below.

$ awk -f else-program data.txt

Источник

Оцените статью
Adblock
detector