Linux Learning notes-Regular expressions

Source: Internet
Author: User
Tags egrep

&& and success-execution unsuccessful-do not execute

|| or success-not performing unsuccessful-execution

; (not) Success-execution


grep--color ' root '/etc/passwd highlight

echo "Alias cg= ' grep--clort '" >> ~/.BASHRC

-N Display number of rows

-C counts a few rows

-V does not contain keyword

-A 2 shows the next two lines

-B More than 2 shows the last two lines

-C 2 more display the upper and lower lines

-R ' root '/etc/* directory search keywords, with file directory name, content

-RH does not show the front directory path

CG ' [0-9] ' 1.txt

CG ' [a-za-z] ' 1.txt

Cg-v ' [a-za-z] ' 1.txt does not contain

CG ' ^[a-z] ' 1.txt starts with a letter

CG ' [^0-9] ' 1.txt does not contain a pure number, does not contain a blank line

CG ' ^[^0-9] ' 1.txt lines that do not start with a number, do not contain blank lines

CG ' ^$ ' 1.txt show Blank line

CG ' R.O ' 1.txt any one character

CG ' R*o ' 1.txt 0 or more front character O also conforms to

CG ' R.*o ' 1.txt r start O results count, no matter what in the middle

CG ' R\?o ' 1.txt de-semantic characters ==cg-e ' r?o ' 0 or 1 preceding characters

GREP-E ==egrep E instead of de-righteousness


Egrep ' R+o ' 1.txt 1 or more preceding characters


+ ? () {} | need to use EGREP or grep-e or de-righteousness \

Egrep ' Root|nologin ' 1.txt or

grep ' Root ' 1.txt | grep ' Nologin ' and

Egrep ' (RR) + ' 1.txt overall match ==grep-e ' (RR) + ' 1.txt

Egrep ' (RR) {1,3} ' 1.txt matches 1 to 3 times

Egrep ' (RR) {5} ' 1.txt matches 5 times


SED differs from grep with no support for color grep all features sed support

Display function, n r//P


Sed-n ' P 1.txt print first 10 lines ' 1,10 ' 3,$ '

Sed-n '/root/' P 1.txt contains the root row

Sed-n '/r.ot/' P 1.txt '/r*o/'/r.*o/'

Sed-n '/r\?ot/' P 1.txt '/r\+o/'/root\|nologin/'/\ (oo\) \+/'

Sed-n '/root/p; /mail/p ' passwd show root or mail line, all contained, display multiple times

= = = Sed-n-E '/root/p '-e '/mail/p ' passwd encounter an order

Sed-n-R '/root|mail/p ' passwd will not be displayed multiple times


R instead of de-justification

Sed-r-n '/(OO) +/' P 1.txt no need to be de-justified

'/[0-9]/'/[^0-9]/' pure number '/[^a-za-z]/' pure letter '/^$/' empty line


Delete function, no need to de-justification and R, keyword required//, no keywords do not need///

Sed '/^$/' d 1.txt Delete empty line '/[0-9]/'/[a-za-z]/'


Sed ' 1,19 ' d 1.txt Delete 1 to 19 rows does not change file contents

Sed-i ' 1,19 ' d 1.txt change file contents wc-l 1.txt not recommended, preview before use, or CP


Find and replace, similar to Vim, @#

Sed ' 1,10s/nologin/login/g ' 1.txt 1 to 10 lines replace-I directly modify content

Sed ' 1,10s/\/sbin\/login/login/g ' 1.txt de-righteousness

Sed ' 1,10s#/sbin/login#login#g ' 1.txt no need to be de-justified

Sed ' 1,[email protected]/sbin/[email protected]@g ' 1.txt no need to de-righteousness

Sed ' [email protected]/sbin/[email protected]@g ' 1.txt Replace All

Sed ' [email protected]^.*[email protected]@g ' 1.txt all lines replaced by & for a full line

Sed ' [email protected]^.*[email protected]&[email protected] ' all line endings added

Sed ' [email protected]^.*[email protected]& [email protected] ' more spaces

Sed ' s#[0-9]# #g ' Delete all numbers, empty [a-za-z]

Sed ' s#[^0-9a-za-z]# #g ' remove all special characters


Change

Sed-r ' 1,10s# (^[a-za-z0-9]+) (:. *:) (. *$) #\3\2\1#g ' first and last swap positions, not including special symbols

Sed-r ' s/(^.) (.*) (. $)/\3\2\1/g ' passwd first word and last character swap


awk difference segment does not support color, {} grep sed support {} is not supported

Show

Awk-f ': ' ofs= ': ' {print $3,$4} ' passwd OFS specify delimiter


The

awk '/root/' passwd

awk '/root|user/' passwd or, will not be displayed more than once, no need to de-justification

awk '/r*o/' passwd. *. ? +

awk '/(OO) +/' passwd

Awk-f ': ' $1~/r*o/' passwd segment match

Awk-f ': ' $1~/r*o/{print $} ' passwd only shows a segment after matching

Awk-f ': ' $1~/r*o/{print $}; $1~/mail/{print '} ' passwd or, multiple, multi-display

Awk-f ': ' $1~/r*o|mail/{print '} ' passwd, but will not be displayed more than once


Last 2 sections not updated

Linux Learning notes-Regular expressions

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.