Unix shell programming (3)-Regular Expressions of Unix shell

Source: Internet
Author: User

Unix shell programming (3)-Regular Expressions of Unix shell

 

Match any single character: Period (.)
For example, "R." indicates the pattern that matches "R" with any character.

Match the first line: ^
For example, ^ George indicates matching rows starting with George

Match the end of a line: $
For example, contents $ indicates the string contents that matches the end of a row.

Usage of GNU Ed 0.8
[Root @ localhost programs] # ed intro
253
/.../# Any three characters with spaces before and after the search
The UNIX operating system was pioneered by Ken
/# Repeat the previous search
Thompson and Dennis Ritchie at Bell Laboratories
/
In the late 1960 s. One of the primary goals in
/
The design of the UNIX system was to create
/
Environment that promoted efficient Program
/
Developments.
1, $ P
The UNIX operating system was pioneered by Ken
Thompson and Dennis Ritchie at Bell Laboratories
In the late 1960 s. One of the primary goals in
The design of the UNIX system was to create
Environment that promoted efficient Program
Developments.
1, $ S/P. O/XXX/G # Replace all "P. O" with "XXX"
1, $ P # view the replaced result
The UNIX operating system was xxxneered by Ken
Thomxxxn and Dennis Ritchie at Bell Laboratories
In the late 1960 s. One of the primary goals in
The design of the UNIX system was to create
Environment that xxxmoted efficient xxxgram
Developments.
/^ The/# search for rows starting with
The design of the UNIX system was to create
1, $ S/^/>/# insert at the beginning of each line>
1, $
> Developments.
1, $ P # view the inserted result
> The UNIX operating system was pioneered by Ken
> Thompson and Dennis Ritchie at Bell Laboratories
> In the late 1960 s. One of the primary goals in
> The design of the UNIX system was to create
> Environment that promoted efficient Program
> Developments.
1, $ S/^ // # insert a space at the beginning of each line
1, $ P # view results
> The UNIX operating system was pioneered by Ken
> Thompson and Dennis Ritchie at Bell Laboratories
> In the late 1960 s. One of the primary goals in
> The design of the UNIX system was to create
> Environment that promoted efficient Program
> Developments.
//. $/# Search for rows ended with a period
Developments.
1, $ S/$/>/# Add at the end of each line>
1, $ P # view results
The UNIX operating system was pioneered by Ken>
Thompson and Dennis Ritchie at Bell Laboratories>
In the late 1960 s. One of the primary goals in>
The design of the UNIX system was to create an>
Environment that promoted efficient program>
Developments.>
1, $ S/... $ // # It is best to delete two characters in each line
1, $ P # view results
The UNIX operating system was pioneered by Ken
Thompson and Dennis Ritchie at Bell Laboratories
In the late 1960 s. One of the primary goals in
The design of the UNIX system was to create
Environment that promoted efficient Program
Developments.

Match one of the character groups: [...] Structure
/The/matches the rows containing
[TT] he matches the or
For example:
1, $ S/[aeiouaeiou] // G # Delete All vowels
1, $ P
Th NX prtng running M ws pnrd by kN
Thmpsn nd dnns rtch t BLL lbrtrs
N th lt 1960 s. n f th prmry Gls n
Th dsgn F th NX running M ws t CRT n
Nvrnmnt tht prmtd ffcnt prgrm
Dvlpmfulfill.

[0-9] matching numbers
[A-Z] matches lowercase letters
[A-Za-Z] matching uppercase and lowercase letters
For example:
1, $ S/[A-Z]/*/G # replace all the larger letters *
1, $ P
* He * NIX operating system was pioneered by * en
* Hompson and * Ennis * itchie at * ell * aboratories
In the late 1960 s. * ne of the primary goals in
The design of the * nix system was to create
Environment that promoted efficient Program
Developments.
[^ A-Z] match any character other than uppercase letters

Matches zero to several characters: asterisks (*)
Replace multiple spaces with one space as follows:
[Root @ localhost programs] # ed lotsapaces
126
1, $ P
This is an example of
File that contains a lot
Of blank spaces
1, $ S/* // G
1, $ P
This is an example of
File that contains a lot
Of blank spaces

Another example is to match all the content between the First E and the last E in a row and replace it with ++:
[Root @ localhost programs] # ed lotsapaces
126
1, $ S/E. * E/++/g
1, $ P
This is an ++ of
File that contains a lot
Of blank spaces

1, $ S/[A-Za-Z] [A-Za-Z] */X/g
1, $ P
X ++ x
X
X

Match the exact number of strings:
XX * Indicates matching at least one consecutive x
Xxx * Indicates matching at least two consecutive x
/{Min, max/}, where: min indicates the minimum number of repetitions, and Max indicates the maximum number of repetitions.

Save the matched string /(.../):
Include the characters in parentheses with the forward backslash to capture strings matching the regular expression.
The captured strings are stored in registers numbered 1 to 9.
As follows:
[Root @ localhost programs] # ed phonebook
155
1, $ P
Alica chebba 973-555-2015
Barbara Swingle 201-555-9257
Liz stachiw 212-555-2298
Susan Goldberg 201-555-7776
Tony iannino 973-555-1295
1, $ S // (. */)/(. */) // 2/1/
1, $ P
973-555-2015 Alica chebba
201-555-9257 Barbara Swingle
212-555-2298 Liz stachiw
201-555-7776 Susan Goldberg
973-555-1295 Tony iannino
The front and back fields are exchanged.

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.