Awk
Tags: linux, beginners, awk
Published: 2023-02-02 06:04:18

Awk is utilised for data manipulation and report generation. The awk command does not require compilation and enables the usage of variables, string functions, numeric functions, and logical operators .

Awk allows programmers to create brief but powerful programmes in the form of statements that specify text patterns to be looked for in each line of a document and the action that should be taken when a match is found . Awk is primarily utilised for processing and scanning patterns . In order to determine whether any files include lines that fit the required patterns, it examines one or more of them . If so, it takes the appropriate steps .

The developers' names Aho, Weinberger, and Kernighan are shortened to Awk .

AWK operations include the following : 

- Line-by-line file scanning 
- Field splitting 
- Input line/field comparison with pattern 
- Executes an action on matched lines

Its often used for Producing prepared reports and transforming data files .

Programming constructs include output line formatting , string and arithmetic operations , conditionals and loops .

## Built-In Variables In Awk

The field variables that divide a line of text into discrete words or parts known as fields are included in Awk as built-in variables and are denoted by the numbers $1, $2, $3, and so on ($0 is the complete line) .

- NR: The NR command keeps track of the number of input records at all times .
- NF: The NF command keeps track of how many fields are present in the current input record .
- FS: Field separator characters are used to separate fields on the input line and are contained in the FS command . Space and tab characters or white space are used as the default . To modify the field separator, FS can be moved to another character (usually in BEGIN) .

### Syntax :

```
awk options 'criteria {action}' input_file > output_file
```
 - -f program-file : Instead of reading from the first command line parameter, this is used to read the AWK programme source from .
 the file program-file.
 - -F fs            : Use fs for the input field separator .

#### Examples:

- Using Awk to print every line from a specified file :
```
$ awk '{print}' <filename>
```

- Printing the lines which match the given pattern in the specific file :

```
$ awk '/pattern/ {print}' <filename>
```

- For splitting Line Into Fields :

The awk command breaks each record, or line, into separate records that are by default separated by whitespace characters and stored in the $n variables . It will be saved in $1, $2, $3, and $4, accordingly, if the line contains 4 words. Furthermore, $0 denotes the entire line . 

```
$ awk '{print $1,$2}' record.txt
```

- Using the built-in NR variables for displaying the line number :
Consider the below example for displaying line number on every line in the text file .

```
$ awk '{print NR,$0}' <filename> 
```

- Using the built-in NF variables for displaying the last field :

```
$ awk '{print $NF}' <filename>
```

- Using NR built-in variables to Display Lines in between range : 

Consider below example to print all the line details from 3 to 6 lines in the file .

```
$ awk 'NR==3, NR==6 {print NR,$0}' <filename>
```

For the given text file:  
```
$cat > a.txt

Anil      Bela    Clara
Tasha     priya   naina
Menaka    sita    krishn
Praveena  ram     dia
```

- Consider the below example To print the first item including the row number(NR) separated with ” – “ from each line in a.txt :
  
  
```
$ awk '{print NR "- " $1 }' a.txt
```

Output :
```sh
1 - Anil
2 - Menaka
3 – Menaka    
4 - Praveena
```

- To return the second column/item from a.txt: 

Consider the below exapmple to return the second column/item from a.txt :

```
$ awk '{print $2}' a.txt 
```
Output:

```
Bela
priya
sita
ram
```

- Printing a non-empty line :
```
awk ‘NF == 0 {print NR}’  <filename>
```
Or

```
awk ‘NF <= 0 {print NR}’  <filename>
```

-  To count the lines in a file :  
```
$ awk 'END { print NR }' <filename>
```