You are on page 1of 6

UNIX Course Module 6

Hands-on: 3
UNIX/Linux Training

support@intellipaat.com
+91-7022374614
US: 1-800-216-8930(Toll Free)
UNIX/Linux Course

AWK Command
Step 1: Let us create a small file with records of the status of two active service in the
network. Here’s what the file looks like

Step 2: Print out the data inside the file, 'data.csv'

$ awk '{print}' data.csv

support@intellipaat.com - +91-7022374614 - US: 1-800-216-8930(Toll Free)


UNIX/Linux Course

Step 3: Print all the records

$ awk '{print $0}' data.csv

Step 4: Print out list by pattern matching

$ awk '/pattern/{print}' data.csv

Step 5: Number out each records/rows in the file

$ awk '{print NR,$0}' data.csv

support@intellipaat.com - +91-7022374614 - US: 1-800-216-8930(Toll Free)


UNIX/Linux Course

Step 6: Number out each fields/columns in the file

$ awk '{print NF,$0}' data.csv

Step 7: Formatting output with Output Record Separator (ORS)

$ awk 'BEGIN{ORS="\n\n"} {print $0}' data.csv

support@intellipaat.com - +91-7022374614 - US: 1-800-216-8930(Toll Free)


UNIX/Linux Course

Step 8: Formatting output fields with Output Field Separator (OFS)

$ awk 'BEGIN{ORS="\n\n"} {print $0}' data.csv

But when it comes to real life problem scenarios, text processing becomes more complex.
Instead of writing the awk command every time of the terminal, having a awk program in a
separate file makes more sense.

Step 9: Create a file to store the awk commands

support@intellipaat.com - +91-7022374614 - US: 1-800-216-8930(Toll Free)


UNIX/Linux Course

Step 10: Now process the data file with the help of awk command file

$ awk -f [command-file-name][data filename]

Now that we are familiar with dealing with small data files, we should be able to process data
from large data files that comes in different formats. Let us see how to extract data from a
zipped file. Since we are using virtual machine we will be mounting a folder from our primary
operating system to the operating system that we are running on the virtual machine. Head on
to the next hands-on for the practical explanation.

support@intellipaat.com - +91-7022374614 - US: 1-800-216-8930(Toll Free)

You might also like