LPIC-1 103.7: Searching text with regular expressions
Filtering out comments, pulling addresses out of a log, editing a copy of
a file with sed, and basic against extended regular expressions.
Before you start
The lab creates /root/regex/access.log, a log with IPv4 addresses in it.
Work as root (sudo -i), and write answers to the files in
/root/lab-answers/.
Tasks
- Show
/etc/ssh/sshd_configwithout comments or blank lines, first with twogrep -vcommands, then with onegrep -Ev '^(#|$)'. Save the result in/root/regex/sshd-active.txt. - Pull every IPv4 address out of
/root/regex/access.logwithgrep -Eoand count each one. Write the counts, asuniq -cprints them, to/root/lab-answers/2. - Copy
/etc/ssh/sshd_configto/root/sshd_config, then usesed -i.bakon the copy to set the port to 2222, and compare the two files withdiff. - Write the same pattern as a basic and an extended regex, and check both
give the same matches in
/root/regex/access.log. Write the basic one on line 1 of/root/lab-answers/4and the extended one on line 2.
Hints
grep -v '^#' FILE | grep -v '^$'.grep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}' FILE | sort | uniq -c.- The port line may be commented out (
#Port 22):sed -i.bak 's/^#\?Port .*/Port 2222/'.diff FILE.bak FILE. - In a basic regex
\{,\(,\|and\+do what{,(,|and+do in an extended one.