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

Example Analysis of grep and regular expressions in Linux

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

Editor to share with you the example analysis of grep and regular expressions in Linux, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!

Introduction to grep

Grep is a powerful text search tool that uses regular expressions to search for text and print matching lines. There are usually three versions of grep: grep, egrep (equivalent to grep-E), and fgrep. Egrep is extended grep,fgrep is fast grep (fixed strings to search for text, does not support references to regular expressions, but the query is extremely fast). Grep is one of the three musketeers of Linux text processing.

How grep is used

Mode of use:

Grep [OPTIONS] PATTERN [FILE...] grep [OPTIONS] [- e PATTERN |-f FILE] [FILE...]

Common options:

-- color=auto: highlight the matching text after shading

-I: ignore the case of characters

-o: only the matching strings are displayed

-v: displays rows that cannot be matched by the pattern

-E: supports regular expressions that use extensions

-Q: silent mode, that is, no information is output

-A #: displays the lines that are matched by the pattern and # lines after them

-B #: displays the rows matched by the pattern and the first # lines

-C #: displays the rows matched by the pattern and the # lines before and after them

Note: when using grep matching, it is necessary to use double quotes (single quotes are strong quotes) to prevent errors from being mistaken for parameters or special commands by the system.

Expand the way grep is used

Mode of use:

Egrep [OPTIONS] PATTERN [FILE...] grep-E [OPTIONS] PATTERN [FILE...]

-I: ignore the case of characters

-o: only the matching string itself is displayed

-v: displays lines that are not matched by the pattern

-Q: silent mode, that is, no information is output

-A #: displays the lines that are matched by the pattern and # lines after them

-B #: displays the rows matched by the pattern and the first # lines

-C #: displays the rows matched by the pattern and the # lines before and after them

-G: supports basic regular expressions

Grep regular expression metacharacter

'^': anchor the beginning of the line

'$': anchor the end of the line

'.': matches any character

'*': matches zero or more previous characters

'\?': match the character in front of it 0 or 1 times

\ +': matches the character in front of it one or more times

'\ m\}': matches the character in front of it m times (\ is an escape character)

'\ {mdirection n\}': matches the characters preceding them at least m times and n times at most

'[]': matches a character within a specified range |'[^] 'matches any single character outside the specified range

'\' or'\ baked: anchor suffixes (available\: match complete words)

'\ (\)': treat multiple characters as a whole

Backward reference: refers to the characters matched by the pattern in the preceding group parentheses

The content matched by the pattern in group parentheses or is automatically recorded in the internal variables by the regular expression engine:

\ 1: pattern matching content between the first left parenthesis and the matching right parenthesis from the left

\ 2: the content of the pattern match between the second left parenthesis and the matching right parenthesis from the left.

Extended regular expressions are slightly different from regular expressions:

'[]': still matches any single character in the specified range; but there are many special matches.

[: digit:] matches any single number

[: lower:] matches any single lowercase letter

[: upper:] matches any single uppercase letter

[: alpha:] matches any single letter

[: alnum:] matches any single letter or number

[: punct:] matches any single symbol

[: space:] matches a single space

The use of escape characters has been eliminated in some places:

'?': match the character in front of it 0 or 1 times

'+': matches the character before it one or more times

'{m}': matches the character in front of it m times (\ is escape character)

'{mdirection n}': matches the characters preceding them at least m times and n times at most

(): one or more characters are bundled together and treated as a whole, and backreferences are used as usual.

'|': or (Note:'C | cat' is C and cat,' (C | c) at is Cat and cat')

Exercise:

1. List the user names of all logged-in users on the current system. Note: if the same user has logged in multiple times, it will only be displayed once.

[root@localhost ~] # who | cut-D''- F1 | uniqroot

2. Take out the relevant information of the user who last logged in to the current system

[root@localhost ~] # id `last | head-1 | cut-d''- f1`uid = 0 (root) gid=0 (root) groups=0 (root)

3. Take out the shell on the current system that users regard as the most default shell.

[root@localhost ~] # cut-donglv'- f7 / etc/passwd | uniq-c | sort-n | tail-1 | cut-d''- f7/sbin/nologin

4. Change the information of the last 10 users with the largest setting of the third field in / etc/passd to uppercase and save it to the / tmp/maxuser.txt file

[root@localhost ~] # sort-tweezer'- k3-n / etc/passwd | tail-10 | tr 'amurz` 'Amurz' > / tmp/maxusers.txt [root@localhost ~] # cat / tmp/maxusers.txt NOBODY:X:99:99:NOBODY:/:/SBIN/NOLOGINSYSTEMD-NETWORK:X:192:192:SYSTEMD NETWORK MANAGEMENT:/:/SBIN/NOLOGINNGINX:X:996:994:NGINX WEB SERVER:/VAR/LIB/NGINX:/SBIN/NOLOGINCHRONY:X:997:995: : / VAR/LIB/CHRONY:/SBIN/NOLOGINPOLKITD:X:998:996:USER FOR POLKITD:/:/SBIN/NOLOGINSYSTEMD-BUS-PROXY:X:999:997:SYSTEMD BUS PROXY:/:/SBIN/NOLOGINDINGJIE:X:1000:1000:DINGJIE:/HOME/DINGJIE:/BIN/BASHJEFF:X:1001:1024:WOSHIDASHUAIBI:/HOME/JEFF:/BIN/BASHEGON:X:1002:1002::/HOME/EGON:/BIN/BASHNFSNOBODY:X:65534:65534:ANONYMOUS NFS USER:/VAR/LIB/NFS:/SBIN/NOLOGIN

5. Fetch the IP address of the current host

[root@localhost ~] # ifconfig | egrep "inet.*broadcast.*" | cut-d''- f10192.168.0.133

6. List the file names of all files in the / etc directory that have ended with .conf, convert their names to uppercase and save them to the / tmp/etc.conf file

[root@localhost ~] # find / etc-name'* .conf'| egrep-o "[^ /] * (\ .conf) $" | tr'amurz 'Amurz' Amurz'> / tmp/etc.conf [root@localhost ~] # cat / tmp/etc.conf RESOLV.CONFCA-LEGACY.CONFFASTESTMIRROR.CONFLANGPACKS.CONFSYSTEMD.CONFVERSION-GROUPS.CONFLVM.CONFLVMLOCAL.CONFASOUND.CONFLDAP.CONFMLX4.CONFRDMA.CONFSMTPD.CONF

7. Displays the total number of subdirectories or files at one level below the / var directory

[root@localhost ~] # ls / var | wc-l

8. Take out the names of the 10 groups with the lowest value in the third field of / etc/group

[root@localhost ~] # sort-t:-K3-n / etc/group | head-10 | cut-dazzlement'- f1rootbindaemonsysadmttydisklpmemkmem

9. Merge the contents of / etc/fstab and / etc/issue files into the same content and save them to the / tmp/etc.test file

[root@localhost ~] # cat / etc/fstab / etc/issue > / tmp/etc.test [root@localhost ~] # cat / tmp/etc.test # # / etc/fstab# Created by anaconda on Sat May 13 10:12:58 2017 October # Accessible filesystems, by reference, are maintained under'/ dev/disk'# See man pages fstab (5), findfs (8) Mount (8) and/or blkid (8) for more info#/dev/mapper/cl-root / xfs defaults 0 0UUID=2789d01a-4e2b-47a5-9c3c-537641648663 / boot xfs defaults 0 0/dev/mapper/cl-swap defaults 0\ SKernel\ r on an\ m are all the contents of the article "sample Analysis of grep and regular expressions in Linux" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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

Internet Technology

Wechat

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

12
Report