The regular expression of the formation of the Great God of Linux (grep,sed)

Source: Internet
Author: User

The regular expression grep command in the form of the great God of Linux

Parameters:

    • -A followed by a number representing after, indicating that the row's subsequent n rows are also listed
    • -B followed by a number, representing the Befer, indicating that the row is listed in front n rows
|-n-A3-B2--color=‘eth‘
    1. Find a specific string
$ 
1   "Open Source" is a good mechanism to develop programs. 2  apple is my favorite food. 3  Football game is not use feet only. 4  this dress doesn‘t fit me. 5  However, this dress is about $ 3183 dollars.^M 6  GNU is free air not free beer.^M 7  Her hair is very beauty.^M 8  I can‘t finish the test.^M 9  Oh! The soup taste good.^M10  motorcycle is cheap than car.11  This window is clear.12  the symbol ‘*‘ is represented as start.13  Oh!     My god!14  The gd software is a library for drafting programs.^M15  You are the best is mean you are the no. 1.16  The world <Happy> is the same with "glad".17  I like dog.18  google is the best tools for search keyword.19  goooooogle yes!20  go! go! Let‘s go.21  # I am VBird
$ ‘the‘

8:i can ' t finish the Test.^m
12:the symbol ' * ' is represented as start.
15:you is the best was mean you are the No. 1.
16:the World was the same with "glad".
18:google is the best tools for search keyword.

2. use brackets to find the set character

$ ‘t[ae]st‘

8:i can ' t finish the Test.^m
9:oh! The Soup taste good.^m
Find a sentence that contains tast or test characters

$ ‘[^g]oo‘

2:apple is my favorite food.
3:football game isn't use feet only.
18:google is the best tools for search keyword.
19:goooooogle yes!
^ Reverse selection, that is, the non-meaning of OO is not the letter of the front of G

$ ‘[^a-z]oo‘

3:football game isn't use feet only.
OO Front is non-lowercase

3. beginning and end of line character ^$

$ grep -n ‘^[a-z]‘ regular_express.txt

2:apple is my favorite food.
4:this dress doesn ' t fit me.
10:motorcycle is cheap than car.
12:the symbol ' * ' is represented as start.
18:google is the best tools for search keyword.
19:goooooogle yes!
20:go! Go! Let ' s go.
^ Put on the outside to indicate the beginning of the line, indicating that the row starts with a lowercase letter

$ ‘^[^a-zA-Z]‘

1: "Open Source" is a good mechanism to develop programs.
21:# I am Vbird
Lines that don't start with a letter

$ ‘\.$‘

1: "Open Source" is a good mechanism to develop programs.
2:apple is my favorite food.
3:football game isn't use feet only.
4:this dress doesn ' t fit me.
10:motorcycle is cheap than car.
11:this window is clear.
12:the symbol ' * ' is represented as start.
15:you is the best was mean you are the No. 1.
16:the World was the same with "glad".
17:i like dog.
18:google is the best tools for search keyword.
20:go! Go! Let ' s go.
$ represents the end of the line, which represents the line ending with '. ', where ' \ ' is an escape character

grep‘^$‘grep  ‘^#‘

# Log all kernel messages to the console.
# Logging Much else clutters up the screen.
#kern. */dev/console
# Log anything (except mail) of level info or higher.
# Don ' t log private authentication messages!
# The Authpriv file has restricted access.
# Log All of the mail messages in one place.
# Log Cron Stuff
# Everybody gets emergency messages
# Save News errors of level crit and higher in a special file.
# Save Boot messages also to Boot.log
Select a non-empty line, and then select the text that begins with #

4. any one. and repeated *


$ grep -n ‘g..d‘ regular_express.txt

1: "Open Source" is a good mechanism to develop programs.
9:oh! The Soup taste good.^m
16:the World was the same with "glad".
' G.. d ' means that there are two consecutive arbitrary characters in the middle of G and D that can match

$ ‘goo*g‘

18:google is the best tools for search keyword.
19:goooooogle yes!
' Goo*g ' before an o must have, after an ' O * ' represents one or more, so for at least one O

grep -n g.*g

1: "Open Source" is a good mechanism to develop programs.
14:the GD Software is a library for drafting programs.^m
18:google is the best tools for search keyword.
19:goooooogle yes!
20:go! Go! Let ' s go.
Lines representing the end of G starting with G

$ grep -n ‘[0-9][0-9]*‘ regular_express.txt

5:however, this dress was about $3183 dollars.^m
15:you is the best was mean you are the No. 1.
Represents a line that starts with a number, followed by 0 to any number

5. Limit the range of consecutive re characters {}


$ grep -n ‘o\{2\}‘ regular_express.txt

1: "Open Source" is a good mechanism to develop programs.
2:apple is my favorite food.
3:football game isn't use feet only.
9:oh! The Soup taste good.^m
18:google is the best tools for search keyword.
19:goooooogle yes!
Represents a row that contains two ' o '

$ ‘go\{2,\}g‘ regular_express.txt

18:google is the best tools for search keyword.
19:goooooogle yes!
The middle of G and G contains more than two O

SED tools

Parameters:

    • -N Quiet mode, listing only specially processed rows
    • -I directly modifies the contents of the file, not the screen output
    • -e sed action edit directly in command line mode
    • -F SED action written in a file
    • -R Extended Regular syntax

function parameters;

    • A new
    • C Replace
    • D Delete
    • I insert
    • P Printing
    • s replacement (and C for different viewing exercises)

      1. Add/Remove in behavioral units

$ ‘2,5d‘
1 root:x:0:0:root:/root:/bin/bash 6  sync:x:5:0:sync:/sbin:/bin/sync 7  shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown 8  halt:x:7:0:halt:/sbin:/sbin/halt 9  mail:x:8:12:mail:/var/spool/mail:/sbin/nologin PS:表示删除第2-5行
$ ‘2a Drink tea or .....\ > drink bear ?‘

1 Root:x:0:0:root:/root:/bin/bash
2 Bin:x:1:1:bin:/bin:/sbin/nologin
Drink tea or .....
Drink bear?
3 Daemon:x:2:2:daemon:/sbin:/sbin/nologin
4 Adm:x:3:4:adm:/var/adm:/sbin/nologin
5 Lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
PS: Add the following string after the second line, with ' \ ' to indicate a new line increment

2. The function of replacing the display in the behavior unit

$ nl /etc/passwd | sed ‘2,5c No 2-5 number‘

1 Root:x:0:0:root:/root:/bin/bash
No 2-5 Number
6 Sync:x:5:0:sync:/sbin:/bin/sync
7 Shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
PS: Replace 2-5 lines with ' No 2-5 number '

$ ‘2,5p‘
2 bin:x:1:1:bin:/bin:/sbin/nologin 3  daemon:x:2:2:daemon:/sbin:/sbin/nologin 4  adm:x:3:4:adm:/var/adm:/sbin/nologin 5  lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin PS:注意这里 ‘-n‘ 安静模式,只列出特殊处理的行
$ /sbin/ifconfig eth1

eth1 Link encap:ethernet HWaddr 6c:ae:8b:21:98:9b
inet addr:10.209.102.43 bcast:10.209.102.127 mask:255.255.255.128
Up broadcast RUNNING multicast mtu:1500 metric:1
RX packets:47103902950 errors:0 dropped:7743 overruns:7743 frame:0
TX packets:11073405019 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:59771891524541 (54.3 TiB) TX bytes:810933823454 (755.2 GiB)

$ /sbin/ifconfig eth1  ‘inet addr‘
     inet addr:10.209.102.43  Bcast:10.209.102.127  Mask:255.255.255.128
$ /sbin/ifconfig eth1  ‘inet addr‘‘s/^.*addr://g‘

10.209.102.43 bcast:10.209.102.127 mask:255.255.255.128
PS: "sed ' s/to replace character/new character/g '"

$ /sbin/ifconfig eth1  ‘inet addr‘‘s/^.*addr://g‘‘s/Bcast.*//g‘

10.209.102.43
PS: In fact, it is equivalent to Hostname-i

$ ‘MAN‘‘s/#.*$//g‘‘/^$/d‘

Manpath/usr/share/man
Manpath/usr/man
Manpath/usr/local/share/man
Manpath/usr/local/man
Manpath/usr/x11r6/man
Manpath_map/bin/usr/share/man
Manpath_map/sbin/usr/share/man
Manpath_map/usr/bin/usr/share/man
Manpath_map/usr/sbin/usr/share/man
PS: First select the line containing ' man ', then replace ' # ', that is, the comment, and then delete the blank line, note that the last empty line is written in a pair of '//'

    • Directly modify the file (dangerous operation)
$ ‘s/\.$/\!/g‘

PS: Directly to the original file in the '. ' Replace all endings with '! ’

$ ‘$a # This is a tese‘

PS: Add a sentence to the last line of the article

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The regular expression of the formation of the Great God of Linux (grep,sed)

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.