Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Regular expressions and four Musketeers

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Regular expression: not a tool program, but a standard basis for string processing, a string that makes a single string search and match a series of symbols to a grammatical rule. The role of regular expression: 1, test string pattern, data validation 2, replace text 3, based on the matching pattern to extract the content of the text file in line with the conditions regular expression: used to find the content of the file, text, string, generally only four Musketeers grep, egrep, sed, awk wildcards: used to find files or directories, ordinary commands are supported. Find regular expression classification: basic regular expression: BRE, common regular expression extension regular expression: ERE Extension and deepening of linux text processing tools for basic regular expressions: vi/vim editor grep (only supports basic regular expressions) egrep (supports extended regular expressions) sed (supports extended regular expressions) awk (supports extended regular expressions) string matching pattern: function: used to check whether a string contains a substring Replace matching substrings or take substrings that meet certain conditions from a string, and so on. Wildcard:? The matching file name contains 0 or 1 character *: matches 0 or more characters, usually with. Begin with find /-name "a?" find /-name "a *" ^ [0-9] + abc$ Note: ^: what begins [0-9]: represents a number, matches a single number +: configures multiple numbers, matches multiple numbers abc$: ends with abc ^ [a-z0-9 numbers -] {3 million 15} $Note: ^ [a-z0-9 cards -]: ^ stands for what begins; Amurz stands for the letter Amurzitor 0-9 represents the number 0-9. The metacharacter represents the basic regular expression of the end tag: metacharacters act as\ escape characters to cancel the meaning of special symbols, such as:!,\ n, etc. ^ matches the start position of the string, such as: world matches the end of the string starting with world, such as: world$ matches the line starting with world. Match any character except\ n (newline) * match the previous subexpression 0 or more times [list] match a character in the list list, for example: [0-9] match any digit [^ list] match a character that is not in the list list, for example: [^ 0-9] match any non-numeric character {n} match the previous subexpression n times For example, [0-9] {2} matches two digits {n,} matches the previous subexpression no less than n times, for example: [0-9] {2,} means that two or more digits {nmagnetic m} match the previous subexpression n to m here, such as: {a Mustz} {2pm 3} matches the metacharacter of a two-to three-digit lowercase letter extended regular expression: metacharacter action + matches the previous expression more than once, such as: go+d Will match at least one o? Match the previous subexpression 0 or 1 times, such as go?d, will match gd or god () will match the string in the sign () as an integer, such as: (xzy) +, will match xyz as a whole more than once the pipe symbol matches the string in the way of or, such as: good pipe symbol great Match good or four swordsmen: grep, egrep, sed, awkgrep tools-I: ignore case-v: take reverse filter out lines containing root in grep 'root' / etc/passwd / / filter lines grep-v' root' / etc/passwd / / filter files that do not contain root note:-v means filter out Do not show grep'r.. d' / etc/passwd / / filter lines with two characters between r and d grep'[^ s] bin' / etc/passwd / / filter lines that are not preceded by s in bin grep'^ $'/ etc/passwd / / filter blank lines grep't [es]'/ etc/passwd / / filter lines containing the strings te or ts grep'0\ {1 \}'/ etc/passwd / / find the line grep-e "root that appears once or more with the number 0 Sshd "filename grep-e 'root'-e' sshd' / etc/passwd / / find lines for root and sshd,-e parameter look for multiple patterns grep'[^ a Muz] ae' / etc/passwd / / filter lines that are not preceded by lowercase letters in ae grep'^ [Amurz] ae' / etc/passwd / / filter lines preceded by lowercase letters note: when using consecutive characters Lowercase [a murz], uppercase [A murz], numbers [0-9] grep 'oaths' / etc/passwd / / matches everything (even blank lines if there are blank lines) Note: oo* matches lines containing at least one o (the first o must appear) The second o may appear 0 or more times) the egrep tool egrep '0lines' / etc/passwd / / matches lines containing at least one 0 egrep'(root | ntp)'/ etc/passwd / / matches lines containing root or nto, egrep 'ro?t' / etc/passwd / / matches lines of rt or rot egrep-v' ^ $| ^ # / etc/passwd / / filters blank lines and lines beginning with # in the file, without blank lines and lines starting with the # sign So there is no output sed tool Overview sed is a text processing tool, reading text content, processing according to specified conditions, such as delete, replace, add, etc. Quite complex text processing operations can be achieved without interaction is widely used in shell scripts, automated processing tasks sed rely on regular expressions sed command syntax: sed-e 'edit instructions' file 1 file 2. Note:-e, there is ";" in the 'edit command' to separate the execution command sed-n-e 'edit instruction' file 1 file 2. -n displays only filtered content sed-I-e 'editing instructions' file 1 file 2. -I: stream editor, read one line to process one line sed command format: edit command format: [address 1, [address 2]] operation [parameter] "address", can be numeric, regular expression, $, if no address represents all line "operations", can be p, d, s, r, w, I and other "parameters" Generally, there is g, which represents that all those who meet the conditions are processed (g all) commonly used operations: P: output specified line d: delete specified line s: substring replacement, format: "line range s / old string / new string / g" r: read specified condition w: save as file I: insert Insert one or more lines before the current line to output the specified line: sed-n 'p' / etc/passwd / / output all the contents to cat sed-n'- 7p' / etc/passwd / / output the seventh line to sed-n'$p' / etc/passwd / / output the last line to sed-n '1line 7 {p N}'/ etc/passwd / / output odd lines from 1x 7 lines to sed-n '1pm 7 {n P}'/ etc/passwd / / output even lines from line 1 to 7 sed-n'1 etc/passwd / / from line 1 Four consecutive lines of output sed-n'/ root/p' / etc/passwd / / will match the lines containing root output sed-n'10 Magna nom p' / etc/passwd / / will output sed-nr'/ ro {1,} t nr p'/ etc/passwd / / match not less than 1 leading character 0 from line 10 to the first one containing The-r parameter supports the expansion of the regular expression rot rootsed-n'/root\ | ntp/p' / etc/passwd / / outputs lines containing root or ntp Note: if a special symbol is encountered, the extension regular requires the escape character "\" sed-n'/root / ='/ etc/passwd / / output containing the line number of the root "=" is used to output line number sed-e '5q' / etc/passwd / / exit after outputting the first five lines of information, Q exit sed-e' 5p 7p 9p'/ etc/passwd / / output 5 lines, 7 lines, 9 lines of information insert eligible rows: insert eligible rows: sed 'root/i admin:x:490:490::/:/sbin/nologin' / etc/passwd / / insert admin:x:490:490::/:/sbin/nologinsed' / root/an admin:x:490:490::/:/sbin/nologin' / etc/passwd / / insert admin on the first line containing the root line / insert admin on the next line containing the root line : x:490:490::/:/sbin/nologinsed '3aADMIN' / etc/passwd / / insert ADMIN after line 3 to delete the line that meets the requirements: sed' 1d' / etc/passwd / / delete the first line sed'$d' / etc/passwd / / delete the last line sed'/ ^ $/ d' / etc/passwd / / delete all blank lines Sed '2root/d' 4d' / etc/passwd / / Delete lines 2 to 4 sed' / root/d' / etc/passwd / / Delete the line containing root The "I" here means to take the reverse operation sed'/ ^ root/d' / etc/passwd / / delete the line that begins with root sed'/ nologin$/d' / etc/passwd / / delete the line ending with nologin and replace it with eligible text: sed's root etc/passwd / / replace all rootnologin in the file with blank plus g to replace all s: all Part content sed'/ root/c admin:x:490:490::/:/sbin/nologin' / etc/passwd / / replace the behavior containing root with admin:x:490.... Sed-n's etc/passwd etc/passwd / / replace the second root of each line with admin sed'/ root/s/root/ROOT/g' / etc/passwd / / replace all bin in lines 1 to 3 with BIN sed's bin / etc/passwd / / insert the string ABC sed's / ^ / # /'/ etc/passwd at the end of each line / / insert # sed'/ root/s / ^ / # /'/ etc/passwd / / insert the line containing root at the beginning of the line # sed'1c ABC' / etc/passwd / / replace the first line with ABC sed'y ABC' / etc/passwd / / replace the root correspondence with ROOT y: corresponding to replace sed'/ 1. /'/ etc/passwd / / replace the root correspondence in lines 1 to 10 with text that meets the criteria for ROOT migration: sed'15 out.txt' test.txt / / Save as sed'5r / etc/reslov.conf' test.txt / / read / etc/reslov.conf content after the fifth line of the current file sed '1jue 5 {H D}; $G'test.txt / / migrate lines 1-5 to the end note: h, copy it to the buffer. G, append to the specified line sed'/ ^ ip/s/ ^ / # / 'test.txt / / insert at the beginning of the line that begins with ip # sed' 1 etc/passwd 5HX 15G 'test.txt execute the command multiple times: sed-ne's etc/passwd etc/passwd / / replace sed-ne' s/root/admin/ with root and bash actions Awk awk p' / etc/passwd / / replaces root and bash actions to modify the contents of the file directly:-I modify the original file directly, save the modified file, and insert the # number at the beginning of each line, directly modify the original file. Awk is also a powerful editing tool, just like sed. Quite complex text manipulation command formats can be implemented without interaction: awk option 'mode or condition {edit instruction}' file 1 file 2awk-f script file 1 file 2 working principle: read text line by line, delimited by default with spaces as delimiters, and save the delimited fields to built-in variables And execute the editing command awk built-in variable FS according to mode or condition: specify the field delimiter for each line of text, default to spaces or tab stops. Note: the default delimiter is space equal to-FNF: the number of fields of the currently processed row NR: the row number of the currently processed row (ordinal) $0: the entire row content of the currently processed row $n: the nth field of the currently processed row (nth column) example: awk-F:'{print $0 NF}'/ etc/passwd / / output the number of fields recorded in the / etc/passwd file separated by colons df-hT | awk'{print $1J'6}'/ / output with the awk intercept command df-hT Format without any conditions Print column 1 and column 6 data awk'{print $0}'/ etc/passwd / / output all content cat / etc/passwd grep "/ etc/passwd sed-n 'p' / etc/passwd / / output all content print text content: awk 'NR==1 NR==3 {print} 'bfile / / output lines 1 to 3 content awk' NR==1 | | NR==3 {print} 'bfile / / output line 1 and line 3 awk' / ^ root/ {print}'/ etc/passwd / / output lines starting with root awk'/ nologin$/ {print}'/ etc/passwd / / output lines awk'(NR > = 1) & & (NR) ending with nologin, less than (

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report