# awk command

write a Bash script that groups visitors by IP and HTTP status code, and displays this data.

Requirements:

* The exact format must be:
    
    * OCCURENCE\_NUMBER IP HTTP\_CODE
        
    * In list format
        
* Ordered from the greatest to the lowest number of occurrences
    
    * See example
        
* You must use `awk`
    

```bash
awk '{ print $1,$9 }' apache-access.log | sort | uniq -c | sort -nr
```

[Link to download apache.log file](https://snowcodes.hashnode.dev/wget-command)

Let's break down the command step by step using a simplified example:

Consider the following sample data:

```bash
192.168.1.1 200
192.168.1.2 404
192.168.1.1 200
192.168.1.3 500
192.168.1.2 200
```

1. `awk '{ print $1,$2 }' apache-access.log`: This `awk` command extracts the first and second fields (IP address and HTTP status code) from each line of the `apache-access.log` file and prints them. Here's the output:
    

```bash
192.168.1.1 200
192.168.1.2 404
192.168.1.1 200
192.168.1.3 500
192.168.1.2 200
```

1. `sort`: This `sort` command sorts the output in ascending order:
    

```bash
192.168.1.1 200
192.168.1.1 200
192.168.1.2 200
192.168.1.2 404
192.168.1.3 500
```

1. `uniq -c`: This `uniq` command filters adjacent matching lines and displays them with a count of their occurrences:
    

```bash
   2 192.168.1.1 200
   1 192.168.1.2 200
   1 192.168.1.2 404
   1 192.168.1.3 500
```

The count represents the number of occurrences of each unique combination of IP address and HTTP status code.

1. `sort -nr`: This `sort` command sorts the output numerically in descending order:
    

```bash
   2 192.168.1.1 200
   1 192.168.1.3 500
   1 192.168.1.2 404
   1 192.168.1.2 200
```

The result is a sorted list where each line shows the count followed by the unique combination of IP address and HTTP status code.

By applying this command sequence to your `apache-access.log` file, you'll get a similar output, which can be helpful for analyzing the frequency of IP addresses and their associated HTTP status codes in the log file.
