EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials Unix Tutorial AWK Command in Unix
Secondary Sidebar
Unix Tutorial
  • Basic
    • What is Unix
    • Uses of Unix
    • Career In Unix
    • Install UNIX
    • Unix Architecture
    • Unix Commands
    • VI Command in Unix
    • AWK Command in Unix
    • Paste Command in Unix
    • SED Command in Unix
    • Sort Command in Unix
    • WHO Command in Unix
    • Unix Operators
    • UNIX Administrator
  • Advanced
    • Array in Unix
    • Functions in Unix
    • Unix File Permissions
    • Unix File System
    • Unix Shell Commands
    • Crontab in Unix
    • egrep command in Unix
    • For Loop in Unix
    • VI Editor in Unix
    • What is Unix Shell
    • Gnome Development
    • UNIX interview questions
    • Cheat sheet for UNIX
    • Unix Sort by Column
    • Find Command in Unix
    • Gnome Version

Related Courses

Linux Training Course

Kali Linux Training

Red Hat Training Course

AWK Command in Unix

AWK Command in Unix

Introduction to AWK Command in Unix

AWK Command in Unix Shell Scripting is used for changing the data’s form and report generating. We can use numeric functions, logical operator, variables and string functions to manipulate the data without compiling. AWK command is generally used for processing the data and scanning the patterns in the file. It tries to scan one or more files to check if it contains the patterns that match the specified pattern required and finally does the required action on it. The AWK command is named from the initials of three people – Aho, Weinberger and Kernighan. They are from AT&T Bell Lab and has also contributed many other command-line prompts in Unix Shell Scripting.

AWK Command is used to perform the below actions:

  • Define variables.
  • Use numeric functions or Logical/Arithmetic operators, Variables or Strings.
  • Apply loops or control flow actions.
  • Generate reports.

Basic Syntax of AWK command:

awk [options] [program] [file]

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

What can we do from AWK Command?

Following are the list of Operations that we can do with AWK Command in Unix:

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,560 ratings)
  • AWK command initially tries to scan a file line by line.
  • Divides each input file to fields to match the pattern.
  • It then tries to compare the input file to the specified pattern.
  • Finally, performs actions on the matched lines.

AWK command is useful for transforming the data files and then generating the reports from them. It is also used for performing arithmetic or logical operations on them to find out the matched patterns.

AWK Command is used to perform the below actions:

  • Define variables.
  • Use numeric functions or Logical/Arithmetic operators, Variables or Strings.
  • Apply loops or control flow actions.
  • Generate reports.

AWK Syntax:

awk [options] [selection action] [input_file]

Examples of How to Use AWK Command in Unix

Let us consider there is a file named ‘testing.txt’, where the contents in the file are below:

Code:

cat testing.txt

Output:

AWK Command in Unix 1

Example #1 – Option ‘print’.

To print all the contents in the file, awk helps to print all the line of the content in the file. In the below example, we have not used any pattern, so actions are applicable to the file lines. By default, action without any pattern argument will print the whole data/lines in the file. Hence, we can see that all the lines in the below file has been printed.

Syntax:

awk '{print}' file_name.txt

Code:

awk '{print}' testing.txt

Output:

AWK Command in Unix 2

Example #2 – Pattern match.

To print the specified pattern in the file, we can use the below syntax. It will list out the pattern matched in the input parameter given and will list out the data in the output.

Syntax:

awk '/pattern/ {print}' file_name.txt

Code:

awk '/student/ {print}' testing.txt

Output:

Pattern match

Example #3 – Splitting lines to fields.

We can also split the records in line with whitespace as its delimiter by default. For example, if we have 4 words in a line, it will be stored as $1, $2, $3 and $4, respectively, and $0 will print the complete line.

Syntax:

awk '{print $n, $m}' file_name.txt

Code:

awk '{print $2, $4}' testing.txt

Output:

Splitting lines to fields

Variables Built-in AWK Command in Unix

  • NF: AWK Command with NF variable is used to count the number of fields in the input parameter passed.
  • RS: AWK Command with RS is used to store the record separator in the current input parameter. By Default, a new line is considered to be a record separator if no specified variable is mentioned.
  • NR: AWK command with its variable NR helps to count the number of input records. Generally, lines are considered as records.
  • FS: While using the FS command, we need to specify the field delimiter. By default, white space is considered a delimiter if no specific delimiter is mentioned. It is used to divide the fields based on the input lines.
  • ORS: AWK command with OFS variable is used to store the output record separator that is separated by AWK command to print the output lines. The new line character is treated as the default record separator.
  • OFS: AWK command with OFS variable is used to store the output field separator when AWK prints the output. A blank space character is treated as a default field separator.

Examples of Variables in AWK Command

Given below are the examples mentioned:

Example #1 – Variable NF.

When we want to print the last line of the record in the file, we can use the awk command with the variable NF.

Syntax:

awk '{print $NF}' file_name.txt

Code:

awk '{print $NF}' testing.txt

Output:

Variable NF

Example #2 – Variable NR.

AWK command with its variable NR helps to count the number of input records. Generally, lines are considered as records.

Syntax:

awk '{print $NR <file separator> $n}' file-name.txt

Code:

awk '{print NR "-" $2}' testing.txt

Output:

AWK Command in Unix 6

Example #3 – Variable OFS.

AWK command with OFS variable is used to store the output field separator when AWK prints the output. A blank space character is treated as a default field separator.

Syntax:

awk 'OFS=”field_separator” {print $n, $m, $o}

Code:

date | awk 'OFS="/" {print $2, $3, $6}'
date | awk 'OFS="-" {print $2, $3, $6}'

Output:

Variable OFS

Example #4 – Variable END.

To count the number of lines in the input file, we can use the END variable with the awk command as shown below.

Syntax:

awk 'END {print NR}' file_name.txt

Code:

awk 'END {print NR}' testing.txt

Output:
AWK Command in Unix 8

Example #5 – Variable BEGIN.

This BEGIN variable is used to set actions before any records have been executed. We can also print any data that we want to print before the records are processed, as below.

Syntax:

awk 'BEGIN { print “print your data”} ' file-name.txt

Code:

awk 'BEGIN {print "Starting of the line"}; {print $2}; ' testing.txt

Output:

AWK Command in Unix 9

Conclusion

AWK Command in Unix Shell Scripting is used for advanced level of text processing. It is generally used for pattern match and as a reporting tool. With the use of awk, we can define a set of actions that are to be executed on the input file. The input data that is passed as a parameter is divided into fields and records.

Recommended Articles

This is a guide to AWK Command in Unix. Here we discuss the introduction; what can we do from AWK command? Examples and variables built-in AWK command in Unix. You may also have a look at the following articles to learn more –

  1. Install UNIX
  2. Sort Command in Unix
  3. Array in Unix
  4. What is Unix?
Popular Course in this category
Linux Training Program (16 Courses, 3+ Projects)
  16 Online Courses |  3 Hands-on Projects |  160+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Kali Linux Training (3 Courses, 3+ Projects)4.9
Red Hat Linux Training Program (4 Courses, 1+ Projects)4.8
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more