AWK In Easy Way

AWK In Easy Way

- 2 mins

One scripting language for data manipulation and report generation is called AWK. When it comes to text processing, there are numerous apps that you may substitute it with. You should first review the AWK man page to gain a better knowledge of the available options and variables. I’ll briefly go over some of the AWK techniques used to analyze reports and text.

AWK Options awk can take options like:-

-F to specify a file separator.
-f to specify a file that contains awk script.
-v var=value

In order to declare a variable, I made a test text file and learned how to use awk in place of other programs like cat, sed, and others.

Thus, for instance, if I have to print the contents of a file, I will use image

If only the first column has to be printed. image

As you can see from the last example, I utilized variables to print any text or column I wanted.

$0 for the whole line.
$1 for the first field.
$2 for the second field.
$n for the nth field.

Using Multiple Commands

Use a semicolon to divide instructions if you want to run more than one, like this: image

Should I need to create a header or a footer for the output, they would look like this: image

Since there are more variables than are often used ($0, $1, $2, and $3), such as:

FIELDWIDTHS Specifies the field width.
RS Specifies the record separator.
FS Specifies the field separator.
OFS Specifies the Output separator.
ORS Specifies the Output separator.

The following example I will replace the white space separator with pipe image

On the other hand, if I need to replace sed in some cases with awk, for example printing the output of file and filter line output`s with word starting with or ending with like following. image

In certain situations, AWK is the best option if you need to filter out a large text file. When doing this method on a PC with limited resources, grep is particularly worthless.

Emad Youssef

Emad Youssef

Sr.Systems Engineer | Pentester | Bug Hunter