Awk Tutorial in Unix,Examples of Using AWK

The Unix operating system is one of the most diverse and powerful operating systems ever devised. Created in the late 1960s, Unix provides a stable platform for many business operations globally to this day. Unix administrators use a robust set of commands to tune and support the Unix operating system, and AWK is a part of the language that processes files of text. In essence, AWK allows one to grab certain bits of data from output in the Unix operating system.
Examples of Using AWK
We will continue to look at this command line "# more samplefile | awk '{print $1}' and discuss how AWK can pull data from the "samplefile" file.

If you were to read "samplefile" on its own, the data output might look like this:

# more samplefile

Device File ALPA Tgt Lun Port CU:LDev Type Serial#
=======================================================
/dev/hai/567hyu 00 00 00 CL5A 07:6c OPEN-V 00030052
/dev/hai/c37t8d0 00 08 00 CL6A 07:6c OPEN-V 00030052

But let's say you only want the first field of data from this output. You could use the following AWK command to accomplish this.

# more samplefile | awk '{print $1}' | more

Device
======================================================
/dev/hai/567hyu
/dev/hai/c37t8d0

You could even take it a step further. Let us say that you only want the number that is listed in the output and not the /dev/hai/ part. In that case you would use the operator "awk --F" to act as a field separator. The great thing is that you can decide the field separator. Here is the command you would use.

# more samplefile | awk '{print $1}' | awk --F / '{print $4}' | more
567hyu
c37t8d0

In this case you are using AWK twice. The first time you are grabbing the first field of data, and the second time you are taking that first field, dividing it at the "/", and then pulling out the fourth field in this new group, which leaves you with only the numbers you wanted.

This was just a short example of how AWK comes in handy so that Unix Administrators can quickly filter and grab out only the most important bits of data.

No comments:

Post a Comment

Please Provide your feedback here