Shell ———— Regular expression Basics and sed

Source: Internet
Author: User
Tags control characters lowercase posix printable characters alphanumeric characters egrep

What is a regular

  A regular is a way to describe a character or string by combining symbols with special meanings, called regular expressions. Or, the regular is the rule used to describe a class of things.

In Linux, wildcard characters are interpreted by the shell, and regular expressions are interpreted by commands, so let's introduce three types of text processing Tools/commands: grep, sed, awk, all of which can be interpreted as regular.

Two grep

Parameters

-N: Show line numbers
-O: Show only matching content
-Q: Silent mode, no output, you have to use $? To determine the success of the execution, that is, there is no filtering to the desired content

-L: If the match succeeds, only the file name is printed, the failure is not printed, usually-rl together, grep-rl ' root '/etc
-A: If the match is successful, the matching row and the subsequent n rows are printed together
-B: If the match succeeds, the matching row and its first n rows are printed together
-C: If the match succeeds, the matching row and its n rows are printed together
--color
-C: If the match succeeds, the number of rows to match is printed
-e: Equals Egrep, extended
-I: Ignore case

-V: Inverse, mismatch
-W: Match word

Example one:

[[email protected] ~]# cat a.txt root123root asdfroot_123rootssroot 123[[email protected] ~]# grep-i "root" a.txt root123 Root asdfroot_123rootssroot 123[[email protected] ~]# grep-w "root" a.txt root 123


grep type
Grep
Fgrep
Pgrep
Egrep

The regular introduction

^ Beginning of the line
$ End of line
. Any single character other than the line break
* 0 or more of the leading characters
. * All Characters
[] Any character within a group of characters
[^] reverse each character within a character group (does not match each character in a group of characters)
^[^] lines that begin with characters within a non-character group
[A-z] lowercase letter
[A-z] capital letters
[A-z] lowercase and uppercase letters
[0-9] Number
\< words are usually separated by spaces or special characters, and successive strings are treated as words.
\> Word Tail

Extended regular sed plus-r parameter or escape
grep plus-e or egrep or escape
AWK direct support without {N,M}
can use--posix support
[[email protected] ~]#&nbs P; awk '/ro{1,3}/{print} '/etc/passwd
[[email protected] ~]#  awk--posix '/ro{1,3}/{print} ' /etc/passwd

sed-n '/roo\?/p '/etc/passwd 
Sed-rn '/roo?/p '/etc/passwd
? Leading character 0 or one
+ leading character one or more
Abc|def ABC or def
A (bc|de) f ABCF or Adef
x\{m\} x appears m times
x\{m,\} X appears m times to multiple times (at least m times)
X\{m,n\} X appears m times to N times

POSIX-defined character classification

[: Alnum:] alphanumeric characters. The
Match range is [a-za-z0-9]
[: Alpha:] alphabetic characters.
The match range is [a-za-z]
[: blank:] Space or tab characters.
The match range is a space and the TAB key
[: Cntrl:] Control characters.
Match control keys such as ^m to press CTRL + V and press ENTER to output
[:d igit:] Numeric characters.
Match all numbers [0-9]
[: graph:] characters that is both printable and visible. (A space is the print-
able, but not visible, while an A is both.)
Match all visible characters without spaces and tab is all the symbols you can see with your eyes on your keyboard in a text document
[: Lower:] lower-case alphabetic characters.
lowercase [A-z]
[:p rint:] Printable characters (characters that is not control characters.)
Match all visible characters including spaces and tab
to all symbols printed on the paper
[:p UNCT:] punctuation characters (characters that is not letter, digits, con-< Br>trol characters, or space characters).
Special input symbol +-=) (*&^%$#@!~ ' |\ "' {}[]:;? />.<,
Note that it does not contain spaces and tab
This collection does not equal ^[a-za-z0-9]
[: space:] space characters (such as space, tab, and FormFeed, to Name a few).

[: Upper:] Upper-case alphabetic characters.
uppercase [A-z]
[: Xdigit:] Characters that is hexadecimal digits.
16 binary number [0-f]

Three SED

Sed
Stream editor Stream editer, which is a handler for the behavior unit

Sed Stream Editor Stream editer

Grammar
sed [options] ' command ' in_file[s]
Options section
-N
-E
-I.
-F
Command section
' [Address 1, Address 2] [function] [parameter (tag)] '

method of addressing 1. Number 2. Regular
Digital
Decimal number
1 single row
1,3 range from first row to third row
2,+4 matches rows after several rows
4,~3 from line fourth to multiples of next 3
Three rows per interval from the second line
$ tail Line
1! Except the first line
Regular
The regular must be wrapped up in//
Extended regular requires the-R parameter or escape

Digital addressing: sed-n ' 1p '/etc/passwd

Regular addressing: Sed-n '/^root/p '/etc/passwd

The regular introduction

^ Beginning of the line
$ End of line
. Any single character other than the line break
* 0 or more of the leading characters
. * All Characters
[] Any character within a group of characters
[^] reverse each character within a character group (does not match each character in a group of characters)
^[^] lines that begin with characters within a non-character group
[A-z] lowercase letter
[A-z] capital letters
[A-z] lowercase and uppercase letters
[0-9] Number
\< words are usually separated by spaces or special characters, and successive strings are treated as words.
\> Word Tail

Extended regular plus-r parameter or escape
Sed-n '/roo\?/p '/etc/passwd
Sed-rn '/roo?/p '/etc/passwd
? A leading character 0 or one
+ Leading character one or more
ABC|DEF ABC or DEF
A (Bc|de) f ABCF or Adef
x\{m\} X appears m times
x\{m,\} X appears m times to multiple times (at least m times)
x\{m,n\} X appears m times to N times


Function
Delete and change
A rear plug
C Replace
I front insert
D Delete
Input/Output
P Print matching line general and-n parameter to mask default output
R read in from file
W Write to File
Control flow
! Command Counter example: 1!d Delete rows other than the first row
{} command combination commands are separated by semicolons {1h; G} can be understood as an alternative to the-e parameter

= Print line number (enter the number of the line, not the number of times the line is processed) For example: Sed-n ' 2{=;p} ' infile
N read into the next line to the pattern space example: ' 4{n;d} ' deletes line 5th
N instead, append the next line to the pattern space, and then apply the following command to the current row and the next row

Replace
s string Substitution s/old/new/

$ Sed-n ' s/root/abcdef/p '/etc/passwd

Abcdef:x:0:0:root:/root:/bin/bash

Operator:x:11:0:operator:/abcdef:/sbin/nologin

$ Sed-n ' S/ROOT/ABCDEF/GP '/etc/passwd

Abcdef:x:0:0:abcdef:/abcdef:/bin/bash

Operator:x:11:0:operator:/abcdef:/sbin/nologin

$ Sed-n ' s/root/abcdef/2p '/etc/passwd

Root:x:0:0:abcdef:/root:/bin/bash

$ Sed-n ' s/root/abcdef/3p '/etc/passwd

Root:x:0:0:root:/abcdef:/bin/bash

$ Sed-n ' S/ROOT/ABCDEF/GP '/etc/passwd

Abcdef:x:0:0:abcdef:/abcdef:/bin/bash

Operator:x:11:0:operator:/abcdef:/sbin/nologin

$

\ (\) Save matched characters for reverse reference \ n use up to 9 label label order from left to right
& Replace, use when no label is defined (reverse reference)

Shell ———— Regular expression Basics and sed

Related Article

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.