O-Linux&Shell-W6
Regular expression exercises (10 points total)
Regular expression related exercises
1. Establishment of empty files (0.5 points)
Use individual users to create the following files at once in the user's home directory ~:
class1
and
class2
no
minna
please
hand
in
your
homework
before
November
5th
2. Simple string orientation query practice (0.5 points)
Query the user's home directory ~ the file whose file name contains the s1 string, and redirect the query result to the myhomework file.
3. Start marker practice. (0.5 points)
Query the files in the user's home directory ~ whose file names start with the letter a, and redirect the query results incrementally to the myhomework file.
4. End identifier practice. (0.5 points)
Query the file with a two-digit number in the file name under the user's home directory ~ and redirect the query result to the myhomework file incrementally.
5. Practice using the start and end identifiers together. (0.5 points)
Query the file in the user's home directory ~ the file name starting with the letter n and ending with the letter o, with no characters in the middle, and redirect the query result incrementally to the myhomework file.
6. Practice using fixed character length query conditions. (1 point)
Query the file with a length of 5 in the file name under the user's home directory ~, and redirect the query result to the myhomework file incrementally.
7. Practice of directional digit query. (1 point)
Query the files in the user's home directory ~ the fourth digit of the file name is the letter a, and redirect the query results to the myhomework file incrementally.
8. Practice query conditions for specific multi-characters. (1 point)
Query the file names under the user's home directory ~ with the letter h and the letter d, and the letter h before the letter d, and redirect the query results to the myhomework file incrementally.
9. Pipeline nested combined query (0.5 points)
Query the user's home directory ~ the file name starts with i and has a length of 2 files, and redirect the query results to the myhomework file incrementally.
10. Pipeline nesting combination is not a set query exercise (1 point)
Query the file in the user's home directory ~ the file name is not in an, pq, st, vx, z, and the file name length is 4, and the query result is incrementally redirected to the myhomework file.
11. Query practice for any number of characters appearing a fixed number of times (1 point)
Query the file that appears twice in the file name of the user's home directory ~, which can be continuous or discontinuous, and redirect the query result incrementally to the myhomework file.
12. Query practice with multiple query conditions. (1 point)
Query files with be or re strings in the file name under the user's home directory ~, and redirect the query results to the myhomework file incrementally.
13. Free inquiry exercises. (0.5 points)
Identify the 5th file under the user's home directory ~ in any way. Note that you can't directly use the string query. The content of the regular expression must be reflected, and the result can only have 5th, but no other results. Incremental redirection to the myhomework file.
14. Use any view command to view the contents of the myhomework file. (0.5 points)
step01:
Create multiple files at once:
touch class1 and class2 no minna please hand in your homework before November 5th
step02:
command have used ls
to view files in the current directory
grep
crawled content, search matching
|
piping, use a word to describe what is the pipeline: the previous command is supposed to be output to the screen as the data is the standard input of a command post
ls | grep 's1' >> myhomework
#此处用>重定向会更好: ls | grep 's1' > myhomework
step03: The
file name starts with a:
^
as the first prompt of the line
ls | grep '^a' >> myhomework
step04:
$
as the end of line prompt
ls | grep '2$' >> myhomework
step05:
.
Represents any character, *
matching the preceding character appears any number of times 0-n times
ls | grep '^n.*o$' >> myhomework
step06: The
length of the file name is 5, and there are multiple combinations: the
{n}
function is to make the character in front of it match n times, here is repeated 5 times:
ls | grep '^.\{5\}$' >> myhomework
#或者用通配符
ls | grep ????? >> myhomework
step07:
any of the following lines can be achieved
ls | grep '^…a.*' >> myhomework
#或者用$限制
ls | grep '^…a.*$' >> myhomework
#或者用{}重复匹配
ls | grep '^.\{3\}a.*' >> myhomework
#或者
ls | grep '^.\{3\}a.*$' >> myhomework
step08:
any of the following commands can be achieved
ls | grep '^.*h.*d.*$' >> myhomework
#或者不用提示符约束限制
ls | grep '.*h.*d.*' >> myhomework
#或者用通配符
ls | grep [h]*[d] >> myhomework
step09:
any of the following commands can be achieved
ls | grep '^i.$' >> myhomework
#或者
ls | grep '^i.\{1\}$' >> myhomework
step10:
ls | grep '^[^a-np-qs-tv-xz]\{4\}$' >> myhomework
step11:
any of the following commands can be achieved
ls | grep '^.*o.*o.*$' >> myhomework
#或者
ls | grep 'o.*o' >> myhomework
step12:
ls | grep 'be\|re' >> myhomework
step13:
Free to play, just meet the conditions, for example:
ls | grep '^[1-5].*[h-t]\{2\}' >> myhomework
#或者
ls | grep '^[0-9][a-z]\{2\}' >> myhomework
step14:
cat myhomework