2019 oplossingen labo 3 - Lars Lemmens

Met dank aan de Github van Martijn en natuurlijk Lars Lemmens

Labo 3

Match one or more d characters: d dd ddd dddd "

'user@:~$' • grep -P 'd+' file

'user@:~$' • cat file | grep -P 'd+'

Color and colour are different spellings of the same word.

Color is the preferred spelling in American English, and colour is preferred in all other main varieties of English. The following regex matches both. "

'user@:~$' • grep -P 'colou?r' file

Match wh following by one, two or three e character: 'whe whee wheee'

'user@:~$' • grep -P 'whe{1,3}' file

Match either gray or grey

'user@:~$' • grep -P '{gray|grey}' file

Let's say you want to match a text like !abc! or !123!. Only these two are possible, and you want to capture the abc or 123.

'user@:~$' • grep -P '!(abc|123)!' file

Search /usr/share/dict/dutch for all words which end in 'vuur', without matching the word 'vuur' itself.

'user@:~$' • grep -P '^.+vuur$' /usr/share/dict/dutch

List all words which contain 32 or more letters.

user@:~$' • grep -P '^.{32}$' /usr/share/dict/dutch

List all words starting with the letter 'b', ending with the letter 'l' which are exactly 4 letters long. Capitalize the output

'user@:~$' • grep -E '^b.{2}l$' /usr/share/dict/dutch | tr [:lower:] [:upper:]
    
'user@:~$' • perl -ne 'print uc if /^b.{2}l$/' /usr/share/dict/dutch

List all palindromes with exactly 4 characters

```
'user@:~$' • grep -P '^([A-Za-z])([A-Za-z])\2\1$' /usr/share/dict/dutch

'user@:~$' • perl -lne 'print if $_ eq reverse && length($_) eq 4' /usr/share/dict/dutch
```

Exercise 1:

List all words out of the file /usr/share/dict/dutch which contain 4 or 5 consecutive vowels.

```
'user@:~$' • cat /usr/share/dict/dutch | grep -P '[aeiou]{4,5}'
```

Exercise 2:

Count the number of words out of the file /usr/share/dict/dutch which which start with 'ver', and end with 'en'. ('verplaatsen' is ok, 'overkomen' and 'vertrek' are not ok)

```
'user@:~$' • grep -P '^ver.*en$' /usr/share/dict/dutch | wc -l
```

Exercise 3:

In those annoying night television games people must guess words. Given are all the letters the word consist of and a list of dots, one for every letter.

E.g. We are looking for a 23 letter word with a 'v' in position 8: '.......v...............'. Use the letters 'enrtiscau' for the other positions."

```
'user@:~$' • cat /usr/share/dict/dutch | grep -P '[enrtiscau]{7}v[enrtiscau]{15}'
```

Exercise 4:

Show 'System load = X Y Z' replacing X, Y and Z by the values displayed by the uptime command.

```
'user@:~$' • echo "System load =" $(uptime | awk -F': ' '{print $2}'| tr -d ',')
```

Exercise 5:

List the usernames of the students who logged in from outside of the UCLL network.

```
'user@:~$' • who | grep -Pv '(.*)\s+pts.*\s+\(10\.|tmux' | awk '/^r[0-9]/{print $1}' | sort -u
```

Exercise 6:

How many times have students logged into leia from outside the UCLL network?

```
'user@:~$' • last | grep -Pv '.*?pts.*?\s+10\.' | awk '/^r[0-9]/{print $1}' | wc -l
```

Exercise 7:

Show the file /etc/debconf.conf on screen without comment lines (i.e. lines starting with a #)

```
'user@:~$' • cat /etc/debconf.conf | grep -vP '^#'
```

Exercise 8:

List all unique IP addresses that contacted your Apache web server.

```
'user@:~$' • cat /var/log/apache2/wiki-ssl-access.log | awk '{print $1}' | sort -u
```

Exercise 9:

List all unique IP addresses that contacted the ssh daemon of your Apache web server.

```
'user@:~$' • cat /var/log/auth.log | grep -oP 'sshd.*?\K[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | sort -u
```

Exercise 10:

Create a regular expression to match all words in the dictionary /usr/share/dict/dutch which with 10 unique letters.

```
'user@:~$' • cat /usr/share/dict/dutch | grep -P '^.{10}$' | grep -vP '(.).*\1'
```

Exercise 11:

To combat spam, a mail server administrator wants to reject mail coming from home users. The IP addresses home users always seem to connect from have hostnames like this: ip.domain. Create a regex which matches all these host names.

```
'user@:~$' • cat mail.log | grep -oP 'NOQUEUE: reject: RCPT from \K[0-9]{1,3}.*?\[' | tr -d '['
```

Exercise 12:

List all unique firefox 2.0.0.x versions used to connect to www.khleuven.be. Use log file /home/logs/apache_google.log. "

```
'user@:~$' • cat apache_google.log | grep -oP 'Firefox\/2\.0\.0\.[0-9]+' | sort -n -t '.' -k4 -u
```

Exercise 13:

List all words with 14, 15 or 16 unique letters.

```
'user@:~$' • for foo in 14 15 16; do echo "Words with $foo letters:" $(echo "grep -vP '(.).*\1' /usr/share/dict/dutch | grep -P '^.{$foo}$'"| sh);done

'user@:~$' • for foo in 14 15 16; do echo "Words with $foo letters:" $(grep -vP '(.).*\1' /usr/share/dict/dutch | grep -P "^.{$foo}$");done

'user@:~$' • for foo in 14 15 16; do echo "Words with $foo letters:" && echo "grep -vP '(.).*\1' /usr/share/dict/dutch | grep -P '^.{$foo}$'"| sh;done

'user@:~$' • for foo in 14 15 16; do echo "Words with $foo letters:" && grep -vP '(.).*\1' /usr/share/dict/dutch | grep -P "^.{$foo}$";done
```

Revision #1
Created 17 June 2021 14:13:47 by Jasper G.
Updated 3 December 2021 22:13:09 by Jasper G.