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.

Awk Tutorial: Understand Awk Variables with 3 Practical Examples

Linux Awk Tutorial - User-defined and Built-in Awk Variables

In this article let us review how to define and use awk variables .

  • Awk variables should begin with the letter, followed by it can consist of alpha numeric characters or underscore.
  • Keywords cannot be used as a awk variable
  • Awk does not support variable declaration like other programming languages
  • Its always better to initialize awk variables in BEGIN section, which will be executed only once in the beginning.
  • There are no datatypes in Awk. Whether a awk variable is to be treated as a number or as a string depends on the context it is used in.

Now let us review few simple examples to learn how to use user-defined awk variables.

Awk Example 1: Billing for Books

In this example, the input file bookdetails.txt contains records with fields — item number, Book name, Quantity and Rate per book.

Now the following Awk script, reads and processes the above bookdetails.txt file, and generates report that displays — rate of each book sold, and total amount for all the books sold.

So far we have seen Awk reads the commands from the command line, but Awk can also read the commands from the file using -f option.

Now our Awk script for billing calculation for books is given below.

In the above script,

  • Awk BEGIN section initializes the variable total. itemno, total, book, bookamount are userdefined awk variables.
  • In the Awk Action section, Quantity*bookprice will be stored in a variable called bookamount. Each bookamount will be added with the total.
  • Finally in the Awk END section, total variable will have total amount.

Now execute the book-calculation.awk script to generate the report that displays each book rate and total amount as shown below.

Awk Example 2. Student Mark Calculation

In this example, create an input file “student-marks.txt” with the following content — Student name, Roll Number, Test1 score, Test2 score and Test3 score.

Now the following Awk script will calculate and generate the report to show the Average marks of each student, average of Test1, Test2 and Test3 scores.

In the above Awk script,

  • In the Awk BEGIN section all the awk variables are initialized to zero. test1, test2, test3 and total are user-defined awk variables.
  • In the Awk ACTION section, $3, $4, $5 are Test1, Test2 and Test3 scores respectively. total variable is the addition of 3 test scores for each student. The awk variable test1, test2 and test3 has the total scores of each corresponding test.
  • So in the Awk END section, dividing each test total by total number of records (i.e student) will give you the average score.  NR is an Awk built-in variable which gives total number of records in input.

Awk Example 3. HTML Report for Student Details

In the above two example, we have seen awk variable which has numbers as its values. This example shows awk script to generate the html report for the students name and their roll number.

Use the same student-marks.txt input file that we created in the above example.

We can store the above output, which gives the following html table. In the above script, variable called name and rollno are string variable, because it is used in string context.

Recommended Reading

awk begin variable assignment

If you enjoyed this article, you might also like..

Tagged as: Awk Tutorial Examples , Linux Awk Examples , Linux Awk Tutorial , Unix Awk Examples , Unix Awk Tutorial , Variables in Awk

Comments on this entry are closed.

hi, Can the AWK command work on data that is comma separated or for that matter any other special character(say pipe)?

Anything can be used as the separator by assigning that value to the FS variable, either in the BEGIN block or on the command line:

awk -F\| ‘….’ ## assigment on the command line

awk ‘BEGIN {FS = “|”} …’ ## assigment in the BEGINblock

Thanks Chris.

You can even work with data files with no delimiters by using substring function. Eg. name=substring($0,1,18); id=substring($0,19,5); ,etc. where $0 refers to the whole line.

$0 refers to the whole line of input data.

There is no substring function; the function is substr().

Thanks Chris. Sorry for the slip up.

awwww(k), people should really be using perl(*)

(*) Or other appropriate, but modern, interpreter. python, whatever pleases you.

I want to sort and remove a huge data file with respect to delimiter.

boo. “If you have an awk script that you don’t want anyone to be able to understand, just rewrite it in perl” — Ed Morton in comp.unix shell

You don’t use awk to sort a file; use the corect tool, sort.

Thanks a ton as usual! Learning a lot of cool stuff from the geek stuff 🙂

How to analyse data using multiple files in awk

Ankit, what data do you want to analyse and what information do you want from it?

No i’ve not got any specific project. I just want to try that out and hence asked for the same.

what does awk -v do?

-v var=val Assign the value val to the variable var, before execution of the program begins. Such variable values are available to the BEGIN block of an AWK program.

i disagree with the commenter who said that one should right away use perl/python to solve their problems. I love perl/python but that commenter obviously has little or limited idea of what happens over the command-line which sysadmins interact with every day every minute across AIX, Linux, Solaris, Freebsd and etc. DO NOT right away solve and decide to use perl/python. Understand first what is available with awk/sed/m4/ex/ed then scale up to python/perl when needed..

Useful for me to know what is AWK..

I have a question related to awk-sorting of data files that are located in different folders.

I have got multiple data files (>500 in number) all with the same name ‘file.dat’ but located under different folders (e.g. ~/amit/folder1/file.dat, ~/amit/folder2/file.dat, etc.). Each of these files are essentially one-columned and have the same number of entries (that is, rows). What I want to do is to collect all these files serially and create a new file ‘fileout.dat’ which has file.dat from folder 1 followed by file.dat from folder 2, etc. Had this been a small finite number of files, I could have simply used ‘cat ~/amit/folder/file.dat ~/amit/folder2/file.dat > fileout.dat’ but this is out of the question with more than 500 files to deal with.

Any ideas on now to get this done?

Cheers, Amit

Thanks a lot.. Very useful article

Use cat ~/amit/folder*/file.dat > fileout.dat

Use find / -type f -name file.dat | xargs cat > fileout.dat If you want to restrict your search use -mindepth -maxdepth option with a find command

in my script, i have a variable var1=”Example.001″. in my awk, i want to be able to send output within awk. In my “if” statement, i check for a condition “if (column1==”ok”); then print $0 >> “/tmp/script.$var1.results”. This appears to not work. Does anyone know how to do what i am attempting? what i have to do is actually type in the full path/filename, (print $0 >> “/tmp/script.Example.001.results”) which makes my script(s) not quite as easy to manage. i have a bunch of scripts that have the same commands. my $var1 never exapands.. even if i include ticks around it.. any help is appreciated. tia

Ahh.. i have found a solution!

Next post: How To Add Shutdown / Reboot Functionality to Fluxbox Window Manager for X

Previous post: 2 Books Giveaway: Linux Firewall and Learning jQuery 1.3

Linux 101 Hacks Book

  • Bash 101 Hacks eBook - Take Control of Your Bash Command Line and Shell Scripting
  • Sed and Awk 101 Hacks eBook - Enhance Your UNIX / Linux Life with Sed and Awk
  • Vim 101 Hacks eBook - Practical Examples for Becoming Fast and Productive in Vim Editor
  • Nagios Core 3 eBook - Monitor Everything, Be Proactive, and Sleep Well

POPULAR POSTS

  • 15 Essential Accessories for Your Nikon or Canon DSLR Camera
  • 12 Amazing and Essential Linux Books To Enrich Your Brain and Library
  • 50 UNIX / Linux Sysadmin Tutorials
  • 50 Most Frequently Used UNIX / Linux Commands (With Examples)
  • How To Be Productive and Get Things Done Using GTD
  • 30 Things To Do When you are Bored and have a Computer
  • Linux Directory Structure (File System Structure) Explained with Examples
  • Linux Crontab: 15 Awesome Cron Job Examples
  • Get a Grip on the Grep! – 15 Practical Grep Command Examples
  • Unix LS Command: 15 Practical Examples
  • 15 Examples To Master Linux Command Line History
  • Top 10 Open Source Bug Tracking System
  • Vi and Vim Macro Tutorial: How To Record and Play
  • Mommy, I found it! -- 15 Practical Linux Find Command Examples
  • 15 Awesome Gmail Tips and Tricks
  • 15 Awesome Google Search Tips and Tricks
  • RAID 0, RAID 1, RAID 5, RAID 10 Explained with Diagrams
  • Can You Top This? 15 Practical Linux Top Command Examples
  • Top 5 Best System Monitoring Tools
  • Top 5 Best Linux OS Distributions
  • How To Monitor Remote Linux Host using Nagios 3.0
  • Awk Introduction Tutorial – 7 Awk Print Examples
  • How to Backup Linux? 15 rsync Command Examples
  • The Ultimate Wget Download Guide With 15 Awesome Examples
  • Top 5 Best Linux Text Editors
  • Packet Analyzer: 15 TCPDUMP Command Examples
  • The Ultimate Bash Array Tutorial with 15 Examples
  • 3 Steps to Perform SSH Login Without Password Using ssh-keygen & ssh-copy-id
  • Unix Sed Tutorial: Advanced Sed Substitution Examples
  • UNIX / Linux: 10 Netstat Command Examples
  • The Ultimate Guide for Creating Strong Passwords
  • 6 Steps to Secure Your Home Wireless Network
  • Turbocharge PuTTY with 12 Powerful Add-Ons
  • Linux Tutorials
  • Sed Scripting
  • Awk Scripting
  • Bash Shell Scripting
  • Nagios Monitoring
  • IPTables Firewall
  • Apache Web Server
  • MySQL Database
  • Perl Programming
  • Google Tutorials
  • Ubuntu Tutorials
  • PostgreSQL DB
  • Hello World Examples
  • C Programming
  • C++ Programming
  • DELL Server Tutorials
  • Oracle Database
  • VMware Tutorials

About The Geek Stuff

Copyright © 2008–2023 Ramesh Natarajan. All rights reserved | Terms of Service

  • Getting started with awk
  • Built-in functions
  • Built-in Variables
  • Patterns and Actions
  • Row Manipulation
  • String manipulation functions
  • Two-file processing
  • Useful one-liners - calculating average from a CSV etc
  • Assignment Arguments
  • Command-line variable assignment
  • Local variables
  • Passing parameters to a program using the -v option

awk Variables Command-line variable assignment

Fastest entity framework extensions.

To assign variables from the command-line, -v can be used:

Note that there are no spaces around the equal sign.

This allows to use shell variables:

Also, this allows to set built-in variables that control awk :

See an example with FS (field separator):

Or with OFS (output field separator):

Got any awk Question?

pdf

  • Advertise with us
  • Cookie Policy
  • Privacy Policy

Get monthly updates about new articles, cheatsheets, and tricks.

Awk Tutorial

  • AWK Tutorial
  • AWK - Overview
  • AWK - Environment
  • AWK - Workflow
  • AWK - Basic Syntax
  • AWK - Basic Examples
  • AWK - Built in Variables
  • AWK - Operators
  • AWK - Regular Expressions
  • AWK - Arrays
  • AWK - Control Flow
  • AWK - Loops
  • AWK - Built in Functions
  • AWK - User Defined Functions
  • AWK - Output Redirection
  • AWK - Pretty Printing
  • AWK Useful Resources
  • AWK - Quick Guide
  • AWK - Useful Resources
  • AWK - Discussion
  • Selected Reading
  • UPSC IAS Exams Notes
  • Developer's Best Practices
  • Questions and Answers
  • Effective Resume Writing
  • HR Interview Questions
  • Computer Glossary

AWK - Assignment Operators

AWK supports the following assignment operators −

Simple Assignment

It is represented by =. The following example demonstrates this −

On executing this code, you get the following result −

Shorthand Addition

It is represented by +=. The following example demonstrates this −

In the above example, the first statement assigns value 10 to the variable cnt . In the next statement, the shorthand operator increments its value by 10.

Shorthand Subtraction

It is represented by -=. The following example demonstrates this −

In the above example, the first statement assigns value 100 to the variable cnt . In the next statement, the shorthand operator decrements its value by 10.

Shorthand Multiplication

It is represented by *=. The following example demonstrates this −

In the above example, the first statement assigns value 10 to the variable cnt . In the next statement, the shorthand operator multiplies its value by 10.

Shorthand Division

It is represented by /=. The following example demonstrates this −

In the above example, the first statement assigns value 100 to the variable cnt . In the next statement, the shorthand operator divides it by 5.

Shorthand Modulo

It is represented by %=. The following example demonstrates this −

Shorthand Exponential

It is represented by ^=. The following example demonstrates this −

The above example raises the value of cnt by 4.

It is represented by **=. The following example demonstrates this −

This example also raises the value of cnt by 4.

To Continue Learning Please Login

COMMENTS

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

    You can define Awk variables in the same way you define shell variables as follows: variable_name=value. 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: computer_name="tecmint.com". port_no="22".

  2. How to Use Shell Variables in an AWK Script

    AWK has a couple of options to assign values to internal variables before a script is executed. Notably, the shell and internal variable names can differ. If the variable values contain an escape sequence, AWK interprets it: $ awk -v var='\tval\nue' 'BEGIN { print "var=" var }' var= val ue

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

    Variable after code block. Here we get the variable after the awk code. This will work fine as long as you do not need the variable in the BEGIN block:. variable="line one\nline two" echo "input data" | awk '{print var}' var="${variable}" or awk '{print var}' var="${variable}" file

  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. Assignment Options (The GNU Awk User's Guide)

    Such an assignment has the following form: variable = text. With it, a variable is set either at the beginning of the awk run or in between input files. When the assignment is preceded with the -v option, as in the following: -v variable = text. the variable is set at the very beginning, even before the BEGIN rules execute.

  6. Use of BEGIN and END Rules in Awk

    In the BEGIN block, we've set the valid variable to 1, equivalent to a true value in an awk program. Further, we've defined the isValid() function to validate whether the input line is numeric . Then, we set the valid variable to 0 if at least one non-numeric value is present, else valid remains set to 1 , and the validation message for the ...

  7. Using BEGIN/END (The GNU Awk User's Guide)

    The BEGIN rule prints a title for the report. There is no need to use the BEGIN rule to initialize the counter n to zero, as awk does this automatically (see Variables). The second rule increments the variable n every time a record containing the pattern 'li' is read. The END rule prints the value of n at the end of the run.

  8. How To Use awk In Bash Scripting

    Show every fourth line starting from the first line: awk 'NR%4==1' foo.txt. Print all lines where the 7th column value equals the specified value such as bar: awk '($7 == value)' filename. awk '($7 == "bar")' filename. Here is how to show all the lines which the 7th column value is between a min and a max:

  9. 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.

  10. Awk Command in Linux with Examples

    If the program is large and complex, it is best to put it in a file and use the -f option to pass the file to the awk command: awk -f program-file input-file... In the examples below, we will use a file named "teams.txt" that looks like the one below: Raptors Toronto 58 24 0.707. 76ers Philadelphia 51 31 0.622.

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

    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.

  12. Awk Tutorial: Understand Awk Variables with 3 Practical Examples

    This article is part of the on-going Awk Tutorial and Examples series. Like any other programming languages, Awk also has user defined variables and built-in variables. In this article let us review how to define and use awk variables.. Awk variables should begin with the letter, followed by it can consist of alpha numeric characters or underscore.

  13. 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 ...

  14. awk Tutorial => Command-line variable assignment

    Example. To assign variables from the command-line, -v can be used: $ awk -v myvar="hello" 'BEGIN {print myvar}' hello Note that there are no spaces around the equal sign.

  15. How to use a variable inside an awk statement? [duplicate]

    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

  16. AWK Language Programming

    The variable assignment feature is most useful for assigning to variables such as RS, OFS, and ORS, which control input and output formats, before scanning the data files. It is also useful for controlling state if multiple passes are needed over a data file. For example: awk 'pass == 1 { pass 1 stuff} pass == 2 { pass 2 stuff}' pass=1 mydata ...

  17. Defining a variable in awk

    I don't understand why you had your code in a BEGIN{} block, that would only have been run once and before any lines were read, so NF wouldn't even be defined. Anyway, for the general case, the way to save a substring in a variable in awk is to use substr or sub. So, you could also have done something like:

  18. shell

    Do you want to assign the result of the awk command (i.e. numeric success or fail value) or it's output (i.e. whatever string it prints, if any)? How would you do it for any other command? (hint - it makes no difference that the command in question here is awk) -

  19. AWK

    Output. Counter = 100. In the above example, the first statement assigns value 10 to the variable cnt. In the next statement, the shorthand operator multiplies its value by 10.

  20. shell

    I'm writing a SHELL script that will call an AWK script file printing specific fields based on the options specified by the user from the .sh script. EXAMPLE. -N=field with a list of Names ==> is the field number 2. -A=field with a list of Addresses ==> is the field number 4. -P=field with a list of Prices ==> is the field number 6.