1. Display the lines ending with bash in the /etc/passwd file;
Explanation: Use grep
the 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:
sed
is a streaming text editor for manipulating and converting files.-i
option means to make modifications directly on the original file./^[^#]/
is a regular expression pattern that matches lines that do not#
begin with .s/^/#/
sed
is 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/fstab
For 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/^#//
sed
is 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.log
Used to output the contents ofaccess.log
the file to standard output.cut -d ' ' -f 1
Used 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 1
which means extracting the first field.sort
Used to sort IP addresses.uniq -c
Used to count occurrences of each IP address and display the count in front of each line.sort -nr
It 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 -5
It is used to display only the top five rows, that is, the five IP addresses with the highest traffic.