Skip to main content

LINUX AWK Tutorial Series | Chapter 4

LINUX AWK Tutorial Series | Chapter 4



Hello Guys, Hope you are enjoying the series. Remember practice will only help you grow and learn. So keep practicing!!

In this post, I am going to discuss the Variables and Operators in AWK command.


Variables and Operators

1) User Define Variables

So let's discuss the User-Defined Variables, As mentioned earlier AWK is a combination of a filtering tool and a programming language. So same as other programming languages it will also support Variables, Constants, operation, loops, etc..

Variable is a representation of a value referring to a variable name.
We will see both built-in and user-defined variables. 
I am going to focus on user-defined variables here.

Important Note:
  • No variable declaration is required in AWK same as shell scripting.
  • Variables will be initialized to null string or zero automatically.
  • Variable should begin with letter and can be followed by letters, numbers, underscores.
  • Variable are case sensitive. So be careful. Example: variable "Him" and variable "him" both will be treated separately.

Example 1:
Defining and variable and printing them




 So what we did here, Any Guesses...

I have defined a variable "a" which is storing a number, so no need for any quotes. (even if you give quotes no impact)
Variable "b" and "c" are storing string so they are enclosed within double-quotes.
All variables are separated by a semicolon(;)
Then I am printing the variables a b and c, but remember I used comma(,) between them so that they can use default delimiter to print the data.
Else the output will be like below. Why--It will work as a concatenation on the variables. Numbers and string will concatenate and will become a string.




Now please note you need to pass a file name as well, If you don't pass the system will keep waiting.
But why we have 7 lines in the output??
Reason: AWK processes each line by line of the file and every time, but does an action for printing only variable values.

Try at home: How to print only one line in this output. Feel free to write your answer in the comments section.

What happens when I only give print rather than print a,b,c. The system will not print variable values and just print normal file contents.


Example 2:

See the below screenshot, Awwww What happened here?




If I am doing an arithmetic operation between a number and string "a+b", then it will treat the variable having a string like "0".So in the output 20+0=20 is displayed.

If both are numbers then no issues, It will perform arithmetic operations. Please see the below screenshot.




2) Operators

In the above example, I used a "+" operator. There are multiple operators that can be used for various different purposes.


  • Arithmetic Operators: +,-,*,/,%,++,--
  • Assignment Operators:=,+=,-=,*=,/=
  • Relational Operators: <,>,>=,<=,!=
  • Logical operators: &&,||,!
  • String Comparison: ~,!~
  • String Concatenate: Blank Space

Example 3:

If we want to find all the employees who are Male and having a salary of less than 25000.


See the above screenshot, what I did.

-F--> used because the delimiter for my fields are comma
$2--> My second field contains the M/F column so matching it with "M"
&&--> Using logical operator for joining 2 conditions.
$4--> This is my salary column in the file which has to be less than  25000
print $1--> Printing the name of the employee as it is stored in field one.


Example 4:

Print the records from the employee file along with the sequence number using user-defined variables.



Explanation:

-F--> used because the delimiter for my fields are comma
++x--> Variable x along with arithmetic operator. As mentioned earlier by default variable initializes with 0 and I am adding 1 each time before printing variable x.
print $0--> print all fields


Example 5:

Compare name which starts with Rob and is Male.



I will not explain all syntax, I hope by now you would have understood all other parts. What new I have used here !!

I used $1~"Rob*" or $1~/Rob*/  --> If you remember // was used for pattern search same can be used or you can use double quotes ""
~ --> Is the string comparison operator.


If I want to print all employee name other than starting with Rob



Example 6:

Let's say if I want to find empty files in my employee data file.
Note: I am adding a few empty lines in my file using vi editor.



Explanation:

^$ --> This will search all patterns where the line is empty.^--> start of the line and $--> end of the line. so together it means no data in between.
x=x+1 --> Defining variable which will increase its value every time. By default, the variable value will be 0.
print x --> print value of x.

The same can be done via the below as well.





But the above output is not neat, I only want to display the total count once.

So that introduces our next concept of Begin and End

Begin and End

Begin Meaning: 

It sets an action on pre-processing. This will be executed first before the main execution takes place of the file processing.

End Meaning:

It sets an action on post-processing. This will be executed after the main execution takes place of the file processing.

These are optional procedures. Not required every time.  BEGIN and END keywords has to be written in Capital letters only.


Example 7: 


The total number of empty lines are displayed.

Begin --> WIll just print the string
/^$/{x=x+1} --> will keep adding each line
END --> Once end is encountered then print x will display the last value of x.


Example 8: 

Based on the same let's find the total salary of all employees.




I suppose this is self-explanatory, Please try to understand, if any doubts, feel free to mention in the comment section.

The next session will continue further


If you like please follow and comment

Comments

Popular posts from this blog

WebLogic migration to OCI using WDT tool

WebLogic migration to OCI using WDT tool Oracle WebLogic Deploy Tool (WDT) is an open-source project designed to simplify and streamline the management of Oracle WebLogic Server domains. With WDT, you can export configuration and application files from one WebLogic Server domain and import them into another, making it a highly effective tool for tasks like migrating on-premises WebLogic configurations to Oracle Cloud. This blog outlines a detailed step-by-step process for using WDT to migrate WebLogic resources and configurations. Supported WLS versions Why Use WDT for Migration? When moving Oracle WebLogic resources from an on-premises environment to Oracle Cloud (or another WebLogic Server), WDT provides an efficient and reliable approach to: Discover and export domain configurations and application binaries. Create reusable models and archives for deployment in a target domain. Key Pre-Requisites Source System: An Oracle WebLogic Server with pre-configured resources such as: Applica...

How to Validate TDE Wallet Password in Oracle Database

How to Validate TDE Wallet Password in Oracle Database Validating the Transparent Data Encryption (TDE) wallet password is crucial, especially when ensuring that the password is correct without using the OPEN or CLOSE commands in the database. This blog post explains a straightforward method to validate the TDE password using the mkstore utility. Steps to Validate TDE Wallet Password Follow these steps to validate the TDE wallet password: Step 1: Copy the Keystore/Wallet File Navigate to your existing TDE wallet directory. Copy only the ewallet.p12 file to a new directory. If a cwallet.sso file exists, do not copy it . The absence of cwallet.sso ensures that the wallet does not use auto-login, forcing the utility to prompt for the password. Step 2: Validate Using mkstore Use the mkstore utility to check the contents of the wallet file. The mkstore utility will prompt you for the TDE wallet password, allowing you to validate its correctness. Command Syntax To display the conten...

Rename a PDB in Oracle Database Multitenant Architecture in TDE and Non TDE Environment

Rename a PDB in Oracle Database Multitenant Architecture I am sharing a step-by-step guide to help you rename a PDB. This approach uses SQL commands. Without TDE or encryption Wallet Initial Check Check the Current Database Name and Open Mode: SQL > SELECT NAME, OPEN_MODE FROM V$DATABASE; NAME OPEN_MODE --------- -------------------- BEECDB READ WRITE List Current PDBs: SQL > SHOW PDBS; CON_ID CON_NAME OPEN MODE RESTRICTED ---------- ------------------------------ ---------- ---------- 2 PDB$SEED READ ONLY NO 3 FUAT READ WRITE NO We need to RENAME FUAT to BEE  Steps to Rename the PDB Step 1: Export ORACLE_SID Set the Oracle SID to the Container Database (CDB): export ORACLE_SID=BEECDB Step 2: Verify Target PDB Name Availability If the target PDB name is different from the current PDB name, ensure no service exists with the target PDB name. Run SQL to Check Exi...