Question: How to extract a text part by regexp in linux shell? Lets say, I have a file where in every line is an IP address, but on a different position. What is the simplest way to extract those IP addresses using common unix command-line tools?

Answer: 
For those who want a ready solution for getting IP addresses from apache log and listing occurences of how many times IP address has visited website, use this line:

```
grep -Eo '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' error.log | sort | uniq -c | sort -nr > occurences.txt

```

Nice method to ban hackers. Next you can:

1. Delete lines with less than 20 visits
2. Using regexp cut till single space so you will have only IP addresses
3. Using regexp cut 1-3 last numbers of IP addresses so you will have only network addresses
4. Add `deny from` and a space at the beginning of each line
5. Put the result file as .htaccess
The *awk* example above didn't work for me, and I needed to do it with *awk* specifically, so I came up with this method:

```
$ awk '{match($0,/[0-9]{1,3}+\.[0-9]{1,3}+\.[0-9]{1,3}+\.[0-9]{1,3}+/); ip = substr($0,RSTART,RLENGTH); print ip}' your_sample_file.log

```

You can also just use pipes if you're getting the data from somewhere else. Eg, *ipconfig*

I also realized the method matches invalid IP addresses.

Here is an extended version that only matches valid IPv4 Addresses:

```
$ awk 'match($0, /(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/) {print substr($0, RSTART, RLENGTH)}' sample_file.log

```

Hope it helps someone else.