Linux Regular Expressions

Source: Internet
Author: User

Linux Regular Expressions
Grep: linux Text Processing Sankey: grep: text filtering tool sed: Text editor (line); stream editor awk: Text report editor; Linux awk implementation gawk; grep: global search REgularexpression and Print out the line. purpose: Use a text search tool to search for the target text line by line based on the user's pattern and print the matched rows. Mode: filter condition metacharacters written by metacharacters of regular expressions and text characters: A character does not represent its literal meaning, but is used to indicate wildcard or control functions. There are two types: Basic Regular Expressions: BRE extension Regular Expression Engine: ERE Regular Expression Engine: grep [OPTIONS] PATTERN [FILE...]: Option: -- color = auto: highlight the matched string;-v: The display mode does not match the row;-I: Ignore the case sensitivity;-o: only display the strings that can be matched by the pattern;-q: silent mode;-E: use an extended regular expression; metacharacter of the basic regular expression: character match :.: match any single character; []: match any single character in the specified range; [^]: match any single character in the specified range; [: lower:], [: upper:],... number of times matching: used to specify the characters behind the number of times; *: Any time; abxy xay xxxxxxxy grep "x * y "\?: 0 or 1 time; grep "x \? Y "\ +: 1 or multiple times; \ {m \}: The exact limit is MB; \ {m, n \}: At least m times, up to n times, [m, n] \ {0, n \}: up to n times; \ {m, \}: At least m times ;. *: match any character of any length; position anchoring: ^: First row anchoring; leftmost of the pattern; $: Last Row anchoring; rightmost pattern; \ <, \ B: the beginning of a word; used to represent the left side of the word's pattern; \>, \ B: the end of a word; used to represent the right side of the pattern of a word; ^ $: blank rows; the pattern matching content in the parentheses of the group \ (\) will be recorded by the Regular Expression Engine During execution and saved in the built-in variables; these variables are \ 1, \ 2 ,... \ 1: Starting from the left, the first left bracket, and the matching content of the pattern in the middle of the matching right brace; \ 2 :... backward reference: Use a variable to reference the characters matching the pattern in the group brackets. The extended regular expression: grep family has three commands: grep: Basic regular expression-E: extended Regular Expression -F: the regular expression egrep is not supported. The extended regular expression fgrep is not supported. 1. The line ending with bash in the/etc/passwd file is displayed. [root @ localhost ~] # Grep -- color = auto'bash $ '/etc/passwd root: x: 0: 0: root:/bin/bash wengangwang: x: 1000: 1000: wengangwang:/home/wengangwang:/bin/bash 2. display two or three digits in the/etc/passwd file. [root @ localhost ~] # Grep -- color = auto "[[: digit:] \ {2, 3 \}"/etc/passwd mail: x: 8: 12: mail: /var/spool/mail:/sbin/nologin operator: x: 11: 0: operator:/root:/sbin/nologin games: x: 12: 100: games: /usr/games:/sbin/nologin ftp: x: 14: 50: FTP User:/var/ftp:/sbin/nologin nobody: x: 99: 99: Nobody: //:/sbin/nologin logs: x: 81: 81: System message bus: //:/sbin/nologin polkitd: x: 999: 999: User for polkitd :/: /sbin/nologin 3. display the 'netstat-tan' command result with 'LIST' EN 'followed by a line ending with 0, 1, or multiple blank characters [root @ localhost ~] # Netstat-tan | egrep 'Listen [[: space:] * $ 'tcp 0 0 0.0.0.0: 22 0.0.0.0: * LISTEN tcp 0 0 127.0.0.1: 631 0.0.0.0: * LISTEN tcp 0 127.0.0.1: 25 0.0.0.0: * LISTEN tcp6 0 0: 22: * LISTEN tcp6 0 0 0: 1: 631 ::: * LISTEN tcp6 0 0: 1: 25: * LISTEN 4. Add bash, testbash, basher, and nologin users (the shell of nologin users is/sbin/nologin ); then find the line with the same username as the shell name in the/etc/passwd file [root @ localhost ~] # Useraddbash; useradd testbash; useradd busher; useradd-s/sbin/nologin [root @ localhost ~] # Grep "\ (bash \). * \ 1 "/etc/passwdbash: x: 3002: 3002:/home/bash:/bin/bashtestbash: x: 3003: 3003:/home/testbash: /bin/bash 5. display the default shell and UID of the root, centos, or user1 users on the current system (Please create these users in advance if they do not exist) [root @ localhost ~] # Egrep '^ root | ^ centos | ^ user1'/etc/passwd | cut-d:-f3, 70:/bin/bash3006:/bin/bash3007: /bin/bash [root @ localhost ~] # Egrep -- color = auto' (^ \ <root \> | ^ \ <centos \> | ^ \ <user1 \>) '/etc/passwdroot: x: 0: 0: root:/bin/bashcentos: x: 3006: 3006:/home/centos:/bin/bashuser1: x: 3007: 3007 :: /home/user1:/bin/bash 6. Find/etc/rc. d/init. A word in the d/functions file (which can contain underscores in the middle of the word) followed by a group of parentheses [root @ localhost ~] # Grep '. * () '/etc/rc. d/init. d/forward () {checkpid () {__ readlink () {__ fgrep () {__ umount_loop () {__ umount_loopback_loop () {__ pids_var_run () {__ pids_pidof () {daemon () {killproc () {pidfileofproc () {pidofproc () {status () {Summary () {echo_passed () {echo_warning () {update_boot_stage () {success () {failure () {passed () {warning () {action () {action_silent () {strstr () {co Nfirm () {get_numeric_dev () {is_ignored_file () {is_true () {is_false () {apply_sysctl () {key_is_random () {encrypt () {init_crypto () {7. Use echo to output a path, and then find the base name of the path. Use egrep to retrieve the directory name [root @ localhost ~]. # Echo/etc/passwd | egrep -- color = auto-o "[[: alnum:] + \/? $ "Passwd [root @ localhost ~] # Echo/etc/passwd/| egrep -- color = auto-o "^ \/. * \/"/etc/passwd/8. Find the number between 1-In the ifconfig command execution result [root @ localhost ~] # Ifconfig | egrep '\ <([1-9] | [1-9] [0-9] | 1 [0-9] [0-9] | 2 [0 -4] [0-9] 25 [0-5]) \> 'inet 192.168.88.106 netmask00000000255.0 broadcast 192.168.88.255 inet6 fe80: 20c: 29ff: fe7b: rjc prefixlen 64 scopeid 0x20 <link> ether 00: 0c: 29: 7b: 34: 7c txqueuelen 1000 (Ethernet) RX packets 144034 bytes202904157 (193.5 MiB) TX packets 64975 bytes 4455917 (4.2 MiB) lo: flags = 73 <UP, LOOPBACK, RUNNING> mtu 65536 inet 127.0.0.1 netmask1_0.0.0 inet6: 1 prefixlen 128 scopeid 0x10

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.