Learn How to Use Awk Built-in Variables – Part 10

As we uncover the section of Awk features in this part of the series, we will walk through the concept of built-in variables in Awk. There are two types of variables you can use in Awk, these are; user-defined variables, which we covered in Part 8, and built-in variables.

Built-in variables have values already defined in Awk, but we can also carefully alter those values, the built-in variables include:

  • FILENAME : current input file name( do not change variable name)
  • FR : number of the current input line (that is input line 1, 2, 3… so on, do not change variable name)
  • NF : number of fields in current input line (do not change variable name)
  • OFS : output field separator
  • FS : input field separator
  • ORS : output record separator
  • RS : input record separator

Let us proceed to illustrate the use of some of the Awk built-in variables above:

To read the filename of the current input file, you can use the FILENAME built-in variable as follows:

awk ' { print FILENAME } ' ~/domains.txt 
Awk FILENAME Variable
Awk FILENAME Variable

You will see that the filename is printed for each input line. This is the default behavior of Awk when using the FILENAME built-in variable.

Using NR to count the number of lines (records) in an input file, remember that, it also counts the empty lines, as we shall see in the example below.

When we view the file domains.txt using the cat command, it contains 14 lines with text and empty 2 lines:

cat ~/domains.txt
Print Contents of File
Print Contents of File

To count the number of records, use the NR built-in variable as follows:

awk ' END { print "Number of records in file is: ", NR } ' ~/domains.txt 
Awk Count Number of Lines
Awk Count Number of Lines

To count the number of fields in a record or line, we use the NR built-in variable as follows:

$ cat ~/names.txt
List File Contents
List File Contents

To count the number of fields in each record or line, use the NF built-in variable:

awk '{ print "Record:",NR,"has",NF,"fields" ; }' ~/names.txt
Awk Count Number of Fields in File
Awk Count Number of Fields in File

Next, you can also specify an input field separator using the FS built-in variable, it defines how Awk divides input lines into fields.

The default value for FS is space and tab, but we can change the value of FS to any character that will instruct Awk to divide input lines accordingly.

There are two methods to do this:

  • one method is to use the FS built-in variable
  • and the second is to invoke the -F Awk option

Consider the file /etc/passwd on a Linux system, the fields in this file are divided using the : character, so we can specify it as the new input field separator when we want to filter out certain fields as in the following examples:

We can use the -F option as follows:

awk -F':' '{ print $1, $4 ;}' /etc/passwd
Awk Filter Fields in Password File
Awk Filter Fields in Password File

Optionally, we can also take advantage of the FS built-in variable as below:

awk ' BEGIN {  FS=“:” ; }  { print $1, $4  ; } ' /etc/passwd
Filter Fields in File Using Awk
Filter Fields in File Using Awk

To specify an output field separator, use the OFS built-in variable, it defines how the output fields will be separated using the character we use as in the example below:

awk -F':' ' BEGIN { OFS="==>" ;} { print $1, $4 ;}' /etc/passwd
Add Separator to Field in File
Add Separator to Field in File

In Part 10, we have explored the idea of using Awk built-in variables which come with predefined values. But we can also change these values, though, it is not recommended to do so unless you know what you are doing, with adequate understanding.

After this, we shall progress to cover how we can use shell variables in Awk command operations and, therefore, stay connected to Tecmint.

For those seeking a comprehensive resource, we’ve compiled all the Awk series articles into a book, that includes 13 chapters and spans 41 pages, covering both basic and advanced Awk usage with practical examples.

Product Name Price Buy
eBook: Introducing the Awk Getting Started Guide for Beginners $8.99 [Buy Now]
If this article helped, with someone on your team.

TecMint Weekly Newsletter
Get the Learn Linux 7 Days Crash Course free when you join 34,000+ Linux professionals reading every Thursday.
Check your email for a magic link to get started.
Something went wrong. Please try again.
TecMint has been free for 14 years. Help keep it that way.
Google AI Overviews and tools like ChatGPT have cut into search traffic for independent tech sites like TecMint. Running this site costs over $2,000 every month for hosting, infrastructure, and paying authors to keep the content accurate and tested.

If this article helped you solve a problem, consider buying a coffee. It helps keep TecMint free, supports the authors, and keeps the project going.
☕ Buy Me a Coffee
Aaron Kili
Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

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

Got Something to Say? Join the Discussion...

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.

Free Course
Get a free Linux course before you go.
Subscribe to TecMint Weekly and get the Learn Linux 7 Days Crash Course free. Read by 34,000+ Linux professionals every Thursday.
Something went wrong. Please try again.
Check your email for a magic link to get started.