Tecmint: Linux Howtos, Tutorials & Guides

Learn How to Use Awk Variables, Numeric Expressions and Assignment Operators – Part 8

The Awk command series is getting exciting I believe, in the previous seven parts, we walked through some fundamentals of Awk that you need to master to enable you perform some basic text or string filtering in Linux.

Starting with this part, we shall dive into advance areas of Awk to handle more complex text or string filtering operations. Therefore, we are going to cover Awk features such as variables, numeric expressions and assignment operators.

Learn Awk Variables, Numeric Expressions and Assignment Operators

These concepts are not comprehensively distinct from the ones you may have probably encountered in many programming languages before such shell, C, Python plus many others, so there is no need to worry much about this topic, we are simply revising the common ideas of using these mentioned features.

This will probably be one of the easiest Awk command sections to understand, so sit back and lets get going.

1. Awk Variables

In any programming language, a variable is a place holder which stores a value, when you create a variable in a program file, as the file is executed, some space is created in memory that will store the value you specify for the variable.

You can define Awk variables in the same way you define shell variables as follows:

In the syntax above:

  • variable_name : is the name you give a variable
  • value : the value stored in the variable

Let’s look at some examples below:

Take a look at the simple examples above, in the first variable definition, the value tecmint.com is assigned to the variable computer_name .

Furthermore, the value 22 is assigned to the variable port_no , it is also possible to assign the value of one variable to another variable as in the last example where we assigned the value of computer_name to the variable server.

If you can recall, right from part 2 of this Awk series were we covered field editing, we talked about how Awk divides input lines into fields and uses standard field access operator, $ to read the different fields that have been parsed. We can also use variables to store the values of fields as follows.

In the examples above, the value of first_name is set to second field and second_name is set to the third field.

As an illustration, consider a file named names.txt which contains a list of an application’s users indicating their first and last names plus gender. Using the cat command , we can view the contents of the file as follows:

List File Content Using cat Command

Then, we can also use the variables first_name and second_name to store the first and second names of the first user on the list as by running the Awk command below:

Store Variables Using Awk Command

Let us also take a look at another case, when you issue the command uname -a on your terminal, it prints out all your system information.

The second field contains your hostname , therefore we can store the hostname in a variable called hostname and print it using Awk as follows:

Store Command Output to Variable Using Awk

2. Numeric Expressions

In Awk , numeric expressions are built using the following numeric operators:

  • * : multiplication operator
  • + : addition operator
  • / : division operator
  • - : subtraction operator
  • % : modulus operator
  • ^ : exponentiation operator

The syntax for a numeric expressions is:

In the form above, operand1 and operand2 can be numbers or variable names, and operator is any of the operators above.

Below are some examples to demonstrate how to build numeric expressions:

To understand the use of numeric expressions in Awk , we shall consider the following example below, with the file domains.txt which contains all domains owned by Tecmint .

To view the contents of the file, use the command below:

View Contents of File

If we want to count the number of times the domain tecmint.com appears in the file, we can write a simple script to do that as follows:

Shell Script to Count a String or Text in File

After creating the script, save it and make it executable, when we run it with the file, domains.txt as out input, we get the following output:

Script to Count String or Text

From the output of the script, there are 6 lines in the file domains.txt which contain tecmint.com , to confirm that you can manually count them.

3. Assignment Operators

The last Awk feature we shall cover is assignment operators, there are several assignment operators in Awk and these include the following:

  • *= : multiplication assignment operator
  • += : addition assignment operator
  • /= : division assignment operator
  • -= : subtraction assignment operator
  • %= : modulus assignment operator
  • ^= : exponentiation assignment operator

The simplest syntax of an assignment operation in Awk is as follows:

You can use the assignment operators above to shorten assignment operations in Awk , consider the previous examples, we could perform the assignment in the following form:

Therefore, we can alter the Awk command in the shell script we just wrote above using += assignment operator as follows:

Alter Shell Script

In this segment of the Awk series , we covered some powerful Awk features, that is variables, building numeric expressions and using assignment operators, plus some few illustrations of how we can actually use them.

These concepts are not any different from the one in other programming languages but there may be some significant distinctions under Awk programming.

In part 9 , we shall look at more Awk features that is special patterns: BEGIN and END . Until then, stay connected to Tecmint .

Previous article:

Next article:

Photo of author

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Related Posts

Use Compound Expressions with Awk

How to Use Compound Expressions with Awk in Linux – Part 5

Use Comparison Operators with AWK

How to Use Comparison Operators & Data Filtering with Awk – Part 4

Use Awk to Filter Text or Strings Using Pattern

How to Use Awk for Text Filtering with Pattern-Specific Actions – Part 3

Awk Print Fields and Columns

How to Use Awk to Print Fields and Columns in File – Part 2

Linux Awk Command Examples

How to Filter Text or String Using Awk and Regular Expressions – Part 1

Search Strings in Files Linux

6 Best CLI Tools to Search Plain-Text Data Using Regular Expressions

Got Something to Say? Join the Discussion... Cancel reply

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.

Save my name, email, and website in this browser for the next time I comment.

Next: Assigning Variables on the Command Line , Up: Variables   [ Contents ][ Index ]

6.1.3.1 Using Variables in a Program ¶

Variables let you give names to values and refer to them later. Variables have already been used in many of the examples. The name of a variable must be a sequence of letters, digits, or underscores, and it may not begin with a digit. Here, a letter is any one of the 52 upper- and lowercase English letters. Other characters that may be defined as letters in non-English locales are not valid in variable names. Case is significant in variable names; a and A are distinct variables.

A variable name is a valid expression by itself; it represents the variable’s current value. Variables are given new values with assignment operators , increment operators , and decrement operators (see Assignment Expressions ). In addition, the sub() and gsub() functions can change a variable’s value, and the match() , split() , and patsplit() functions can change the contents of their array parameters (see String-Manipulation Functions ).

A few variables have special built-in meanings, such as FS (the field separator) and NF (the number of fields in the current input record). See Predefined Variables for a list of the predefined variables. These predefined variables can be used and assigned just like all other variables, but their values are also used or changed automatically by awk . All predefined variables’ names are entirely uppercase.

Variables in awk can be assigned either numeric or string values. The kind of value a variable holds can change over the life of a program. By default, variables are initialized to the empty string, which is zero if converted to a number. There is no need to explicitly initialize a variable in awk , which is what you would do in C and in most other traditional languages.

  • Shell Scripting
  • Docker in Linux
  • Kubernetes in Linux
  • Linux interview question
  • Linux Commands

File and Directory Manipulation

  • ls Command in Linux
  • How to Change the Directory in Linux | cd Command
  • How to Display Current Working Directory in Linux | pwd Command
  • How to Create Directory in Linux | mkdir Command
  • rm command in Linux with examples
  • How to Copy Files and Directories in Linux | cp Command
  • How to Move File in Linux | mv Command
  • How to Create an Empty File in Linux | Touch Command
  • How to View the Content of File in Linux | cat Command
  • grep command in Unix/Linux

File Operations and Compression

  • How to Find a File in Linux | Find Command
  • How to Compress Files in Linux | Tar Command
  • Gzip Command in Linux
  • gunzip command in Linux with examples
  • ZIP command in Linux with examples
  • How to Install Zip and Unzip in Linux?
  • How to Make Script Executable in Linux | chmod Command
  • How to Change File Ownership in Linux | chown Command
  • chgrp command in Linux with Examples
  • How to List Running Processes in Linux | ps Command

Network and Connectivity

  • How to Monitor System Activity in linux | top Command
  • How to Kill a Process in Linux | Kill Command
  • How to Find Your IP Address in Linux | ifconfig Command
  • How to Check Network Connectivity in Linux | ping Command
  • How do I use SSH to connect to a remote server in Linux | ssh Command
  • How to Securely Copy Files in Linux | scp Command
  • Wget Command in Linux/Unix
  • curl Command in Linux with Examples
  • How to Compare Files Line by Line in Linux | diff Command
  • Head command in Linux with examples

Text Processing and Manipulation

  • Tail command in Linux with examples
  • How to sort lines in text files in Linux | sort Command

AWK command in Unix/Linux with examples

  • Sed Command in Linux/Unix with examples
  • cut command in Linux with examples
  • tr command in Unix/Linux with examples
  • echo command in Linux with Examples
  • export command in Linux with Examples
  • source Command in Linux with Examples
  • How to Display Command History in Linux | history Command

Help and Information

  • apropos command in Linux with Examples
  • info command in Linux with Examples
  • How to Create and Use Alias Command in Linux
  • uname command in Linux with Examples
  • df command in Linux with Examples
  • du command in Linux with examples
  • How to Mount File System in Linux | mount Command
  • ln command in Linux with Examples

System Administration and Control

  • How to Display Path of an Executable File in Linux | Which Command
  • whereis command in Linux with Examples
  • locate command in Linux with Examples
  • How to Display and Set Date and Time in Linux | date Command
  • cal command in Linux with Examples
  • How to Start, Stop and Restart Services in Linux Using systemctl Command
  • shutdown command in Linux with Examples

User and Group Management

  • init command in Linux with examples
  • How to add User in Linux | useradd Command
  • usermod command in Linux with Examples
  • How to Delete User in Linux | userdel Command
  • How to Create a new group in Linux | groupadd command
  • groupmod command in Linux with examples
  • How to Delete a Group in Linux | groupdel command
  • How to Change User Password in Linux | passwd Command
  • Difference Between su and su - Command in Linux

Privilege and Security Management

  • chroot command in Linux with examples
  • file command in Linux with examples
  • hexdump command in Linux with examples
  • wc command in Linux with examples
  • tee command in Linux with examples
  • script command in Linux with Examples
  • How To Generate SSH Key With ssh-keygen In Linux?

Process Management and Control

  • 'crontab' in Linux with Examples
  • at Command in Linux with Examples
  • nohup Command in Linux with Examples
  • bg command in Linux with Examples
  • fg command in Linux with examples
  • Process Control Commands in Unix/Linux
  • Shell Script to Demonstrate Wait Command in Linux

Data Backup and Synchronization

  • rsync command in Linux with Examples
  • screen command in Linux with Examples
  • uniq Command in Linux with Examples

Awk is a scripting language used for manipulating data and generating reports. The awk command programming language requires no compiling and allows the user to use variables, numeric functions, string functions, and logical operators. 

Awk is a utility that enables a programmer to write tiny but effective programs in the form of statements that define text patterns that are to be searched for in each line of a document and the action that is to be taken when a match is found within a line. Awk is mostly used for pattern scanning and processing. It searches one or more files to see if they contain lines that matches with the specified patterns and then perform the associated actions. 

Awk is abbreviated from the names of the developers – Aho, Weinberger, and Kernighan. 

WHAT CAN WE DO WITH AWK?  

1. AWK Operations:   (a) Scans a file line by line  (b) Splits each input line into fields  (c) Compares input line/fields to pattern  (d) Performs action(s) on matched lines 

2. Useful For:   (a) Transform data files  (b) Produce formatted reports 

3. Programming Constructs:   (a) Format output lines  (b) Arithmetic and string operations  (c) Conditionals and loops 

Options:   

Sample Commands  

Example:  

Consider the following text file as the input file for all cases below: 

1. Default behavior of Awk: By default Awk prints every line of data from the specified file.  

Output:   

In the above example, no pattern is given. So the actions are applicable to all the lines. Action print without any argument prints the whole line by default, so it prints all the lines of the file without failure. 

2. Print the lines which match the given pattern.  

In the above example, the awk command prints all the line which matches with the ‘manager’. 

3. Splitting a Line Into Fields : For each record i.e line, the awk command splits the record delimited by whitespace character by default and stores it in the $n variables. If the line has 4 words, it will be stored in $1, $2, $3 and $4 respectively. Also, $0 represents the whole line.  

In the above example, $1 and $4 represents Name and Salary fields respectively. 

Built-In Variables In Awk

Awk’s built-in variables include the field variables—$1, $2, $3, and so on ($0 is the entire line) — that break a line of text into individual words or pieces called fields. 

  • NR: NR command keeps a current count of the number of input records. Remember that records are usually lines. Awk command performs the pattern/action statements once for each record in a file. 
  • NF: NF command keeps a count of the number of fields within the current input record. 
  • FS: FS command contains the field separator character which is used to divide fields on the input line. The default is “white space”, meaning space and tab characters. FS can be reassigned to another character (typically in BEGIN) to change the field separator. 
  • RS: RS command stores the current record separator character. Since, by default, an input line is the input record, the default record separator character is a newline. 
  • OFS: OFS command stores the output field separator, which separates the fields when Awk prints them. The default is a blank space. Whenever print has several parameters separated with commas, it will print the value of OFS in between each parameter. 
  • ORS: ORS command stores the output record separator, which separates the output lines when Awk prints them. The default is a newline character. print automatically outputs the contents of ORS at the end of whatever it is given to print. 

Examples:  

Use of NR built-in variables (Display Line Number)   

In the above example, the awk command with NR prints all the lines along with the line number. 

Use of NF built-in variables (Display Last Field)   

In the above example $1 represents Name and $NF represents Salary. We can get the Salary using $NF , where $NF represents last field. 

Another use of NR built-in variables (Display Line From 3 to 6)   

More Examples

For the given text file:   

1) To print the first item along with the row number(NR) separated with ” – “ from each line in geeksforgeeks.txt:   

2) To return the second column/item from geeksforgeeks.txt:  

The question should be:- To return the second column/item from geeksforgeeks.txt:

3) To print any non empty line if present   

here NF should be 0 not less than and the user have to print the line number also:

correct answer : awk ‘NF == 0 {print NR}’  geeksforgeeks.txt

awk ‘NF <= 0 {print NR}’  geeksforgeeks.txt

4) To find the length of the longest line present in the file:   

5) To count the lines in a file:   

6) Printing lines with more than 10 characters:   

7) To find/check for any string in any specific column:   

8) To print the squares of first numbers from 1 to n say 6:   

Please Login to comment...

Similar reads.

  • linux-command
  • Linux-text-processing-commands

Improve your Coding Skills with Practice

 alt=

What kind of Experience do you want to share?

Shell Programming and Scripting

Awk variable assignment in a file.

Member Information Avatar

  • I now know how to declare variables in a file.
  • I can now use it to create my perl using: a2p awk5

10 More Discussions You Might Find Interesting

1. shell programming and scripting, optimize multiple awk variable assignment, discussion started by: skysmart, 2. shell programming and scripting, variable assignment, discussion started by: ankur328, 3. shell programming and scripting, same variable assignment, 4. shell programming and scripting, [awk] printing value of a variable assignment from a file, discussion started by: sea, 5. shell programming and scripting, variable assignment inside awk, discussion started by: cica, 6. shell programming and scripting, awk variable assignment-- inside braces or not, discussion started by: treesloth, 7. shell programming and scripting, awk variable assignment issue ksh script, discussion started by: jeevanm, 8. shell programming and scripting, variable assignment using awk, discussion started by: sdosanjh, 9. unix for dummies questions & answers, @ in a variable assignment, discussion started by: rkap, 10. unix for dummies questions & answers, variable assignment for file names., discussion started by: jagannatha, member badges and information modal.

IMAGES

  1. Learn How to Use Awk Variables, Numeric Expressions and Assignment

    unix awk variable assignment

  2. awk command examples in Unix/Linux ~ Tech Blog

    unix awk variable assignment

  3. awk command tutorial in linux/unix with examples and use cases

    unix awk variable assignment

  4. awk command examples in Unix/Linux ~ Tech Blog

    unix awk variable assignment

  5. 21 awk command in linux/unix with examples

    unix awk variable assignment

  6. Learn How to Use Awk Variables, Numeric Expressions and Assignment

    unix awk variable assignment

VIDEO

  1. Variable quantitative discrète

  2. COMP6980

  3. Interactive Simulation of Dynamic Crowd Behaviors using General Adaptation Syndrome Theory

  4. Khulafa-E-Salasa Mujay Parents Say Zeyada Pyaray Hain

  5. Angela Anaconda

  6. BTS (방탄소년단)

COMMENTS

  1. How to Use Shell Variables in an AWK Script

    Next, we directly pass predefined variables via two AWK mechanisms. After that, we turn to command-line arguments. Finally, we use a special internal AWK variable to access the shell environment. Importantly, none of the methods we look at allows for the direct use or assignment of shell variables in AWK.

  2. How do I use shell variables in an awk script?

    As Ed Morton writes and as seen in the above example, the shell variable is expanded by the shell before awk then sees its content as awk -v var='line one\nline two' and so any escape sequences in the content of that shell variable will be interpreted when using -v, just like they are for every other form of assignment of a string to a variable ...

  3. Awk -v variable assignment

    echo "abc xyz" | /usr/bin/awk -v ttl="$2" ' {print ttl}'. This sets the awk variable to the second shell script parameter¹. The awk code then prints that variable, which is empty, for each line of stdin. In the shell context, $2 is the second parameter to a shell script. When you run this directly from the command line there is no "second ...

  4. bash

    Some explanations: parsed=$(...) - spawn a subshell and save the output to stdout within that subshell to the variable parsed awk 'BEGIN{FS=OFS="/"} - invoke awk and set delimiter as / for both input and output. {print $6, $7}' - based on the format of your raw strings, you want to print the 6th (audicerttest) and the 7th (incoming) fields. <<< ${string} is the notation for using herestring

  5. scripting

    You need to export it if you want it passed as an environment variable to awk, or use a=aaaa awk 'BEGIN{print ENVIRON["a"]}'. See the dup question for details. See the dup question for details. - Stéphane Chazelas

  6. Learn How to Use Awk Variables, Numeric Expressions and Assignment

    The simplest syntax of an assignment operation in Awk is as follows: $ variable_name=variable_name operator operand Examples: counter=0 counter=counter+1 num=20 num=num-1 You can use the assignment operators above to shorten assignment operations in Awk, consider the previous examples, we could perform the assignment in the following form:

  7. Awk Command in Linux with Examples

    Built-in Variables # Awk has a number of built-in variables that contain helpful information and allow you to control how the program is processed. Below are some of the most common built-in Variables: NF - The number of fields in the record. NR - The number of the current record. FILENAME - The name of the input file that is currently processed.

  8. set variable inside the awk

    function get_row { awk 'NR==val{print ; exit}' val=$1 list.txt } Here you are passing val with only numbers and if val was contain backslash escape character you will encounter a problem which awk does C escape sequence processing on values passed via -v val= and a shell variable with val="\\n" will change to value with \n by awk.

  9. Using Variables (The GNU Awk User's Guide)

    Variables are given new values with assignment operators, increment operators, and decrement operators (see Assignment Expressions). ... Variables in awk can be assigned either numeric or string values. The kind of value a variable holds can change over the life of a program. By default, variables are initialized to the empty string, which is ...

  10. AWK command in Unix/Linux with examples

    2. Print the lines which match the given pattern. $ awk '/manager/ {print}' employee.txt. Output: ajay manager account 45000. varun manager sales 50000. amit manager account 47000. In the above example, the awk command prints all the line which matches with the 'manager'. 3.

  11. Using AWK to define variables in a shell

    Explanation of the code: The shell pattern var=$( awk '...' ) will assign whatever the awk process will print to the shell variable. Since you can only assign one value by this means you need a different approach for two or more variable assignments. One approach is to let awk print the whole assignment, say awk '{ print "varname=" value ...

  12. linux

    This should be pretty straightfoward and I don't know why I am struggling with it. I am running the following psql command from within a shell script in order to find out whether all indexes have ...

  13. Variable assignment inside awk

    I am trying to match each line that has a MAC address present AND a reserved flag of 00 (awk variables $1 and $2 respectively) OR has a reserved flag of 04 (again awk variable $1). For each of the lines that are matched, I want to pass the awk variables $3 and $5 (of the line in question) out to a shell variable e.g. ip_address=$3 and lease ...

  14. How to assign a value to a variable in awk scripting?

    I have the following script, and I want to assign the output ($10 and $5) from awk to N and L: grdinfo data.grd | awk ' {print $10,$5}'| read N L output from gridinfo data.grd is: data.grd 50 100 41 82 -2796 6944 0.016 0.016 3001 2461. where N and L is suppose to be 3001 and 100.

  15. assign awk output to bash variable

    Hi, I am giving a grep command, and i am getting the output. i want to store it in a variable for eg a = grep '12345' /dir/1/2/log.txt ( the output is number) b= grep 'basic' /dir/1/2/log1.txt (in this case the output is character) so how to assign the output of grep to a variable ...

  16. how to set a variable to multiple values with awk?

    The awk code tests the two columns for the character x and (if true) sets it to the current value of the awk variable x (which is set on the command line). If the columns later are the same, some output is produced. With the given data, this produces the output

  17. awk variable assignment-- inside braces or not?

    2. 3. Assignment is an expression in awk. These two commands are the same only when $0 is true - not 0 or "". In this case x=$0 gives the true value and the action part is executed. Last edited by yazu; 08-24-2011 at 02:32 PM.. Reason: changed $1 to $0. This User Gave Thanks to yazu For This Post: treesloth.

  18. awk assign command result to a variable

    Four things: First: Each time you call rand(), you get a different number.Thus, the word extracted in the printf will be different from the word extracted in the first=. Second: Even if the first point weren't so, it is usually a good idea to call avoid calling a function multiple times if it can be avoided. Third: The line first=($(awk (File[int(rand()*records_count)]) ))

  19. awk variable assignment in a file

    Awk variables are defined upon first use, however, you can explicitly define variables throughout the script but awk provides an initialization procedure called BEGIN. Code : awk ' BEGIN { x=0 } NF != 6 { ++x} END { print "This batch had " x " errors out of ", NR" records" }' yourdata

  20. UNIX for loop using awk to assign a variable

    Awk cannot manipulate your Bash variables, and Bash has no idea what happens inside your Awk script. If you absolutely insist on using Awk for this, you want its output to be a string you can safely eval from your shell.

  21. awk command to replace block between two tags with contents of variable

    I have a variable (call it X) that contains. alpha beta gamma I'm looking for an awk (or sed) expression that removes the text between the two "tag" lines and replaces it with the contents of variable X. The end result should be: bla bla bla tag alpha beta gamma tag bla bla I think I need to do something like this: