Regular Expressions and Text Three Musketeers Exercises

​ 1. Display the lines ending with bash in the /etc/passwd file;

Explanation: Use grepthe tool to search for lines matching/etc/passwd the pattern in the file . is a regular expression where indicates the end of line. Thus, matches lines ending .'bash$'bash$$'bash$'bash

grep 'bash$' /etc/passwd

operation result:


2. Find the line ending with 'LISTEN' followed by 0 or more blank characters in the result of the "netstat -tan" command

method one:

netstat -tan | grep "LISTEN[[:space:]]*$"

 operation result:

Method Two:

netstat -tan | grep 'LISTEN\s*$'

 operation result:

Explanation: LISTEN\s*$'Use a regular expression to match lines ending with "LISTEN" followed by 0 or more whitespace characters. Among them \s*, means to match 0 or more blank characters, and $means to end the line 
​ 3. In the /etc/fstab file, add # to the beginning of the line that does not start with #

Order:

sed -i '/^[^#]/ s/^/#/' /etc/fstab

 

explain:

  • sedis a streaming text editor for manipulating and converting files.
  • -ioption means to make modifications directly on the original file.
  • /^[^#]/is a regular expression pattern that matches lines that do not #begin with .
  • s/^/#/sedis the substitution command for , which ^replaces the beginning ( ) at the beginning of the line with #.

After executing this command, each line of the filesed will be traversed . /etc/fstabFor lines that do not #begin , the numbersed will be added at the beginning of the line . #This adds #a sign

​ 4. Delete # and blank characters at the beginning of all lines starting with # and followed by at least one blank character in the /etc/fstab file

sed -i '/^#[[:blank:]]\+/ s/^#//' /etc/fstab


​Explanation:

  • /^#[[:blank:]]\+/is a regular expression pattern that matches #lines beginning and followed by at least one whitespace character. [[:blank:]]Indicates whitespace characters (including spaces and tabs), +means to match one or more whitespace characters.
  • s/^#//sedis a replacement command for , it #replaces the at the beginning of the line with an empty string, that is, deletes #the number .

5. Count the 5 most visited IPs in apache's access.log

 Order:

cat access.log | cut -d ' ' -f 1 | sort | uniq -c | sort -nr | head -5

explain:

  • cat access.logUsed to output the contents of access.logthe file to standard output.
  • cut -d ' ' -f 1Used to extract the first field in each line, which is the IP address. Here, using a space as a separator -d ' 'means using a space as a separator, -f 1which means extracting the first field.
  • sortUsed to sort IP addresses.
  • uniq -cUsed to count occurrences of each IP address and display the count in front of each line.
  • sort -nrIt is used to sort in reverse according to the count value, that is, sort according to the number of visits from high to low.
  • head -5It is used to display only the top five rows, that is, the five IP addresses with the highest traffic.

Guess you like

Origin blog.csdn.net/m0_68976043/article/details/130873109