Linux regular expression-positioning metacharacters and linux Regular Expressions

Source: Internet
Author: User

Linux regular expression-positioning metacharacters and linux Regular Expressions

Two metacharacters are used to specify whether a string appears at the beginning or end of a row. A single character regular expression that indicates the start. The dollar sign ($) is a regular expression that indicates a single character at the end of a line. These are usually called "locators" because they limit matching to a specific location. For example, you can use the following expression to print rows starting with "First:
^ First
No ^ metacharacters. This expression prints any row containing the First character.

Generally, use VI to input the text to be processed by troff, and do not want spaces to appear at the end of a row. If you want to find and delete them, the following regular expression can match rows with one or more spaces at the end:
□□* $

Troff requests and macros must be input at the beginning of the row. They are two character strings with a period in front. If a request or macro has a parameter, it is usually followed by a space. The regular expression used to search for such a request is:
^ \... □

This expression matches "the line has a period at the beginning, followed by a string with two characters, followed by a space line ".
You can use two consecutive positioning metacharacters to match empty rows, that is, ^ $

You can use this mode to calculate the number of empty rows in the file. In grep, use the count option-c:

$ Grep-c '^ $'ch04
5

This regular expression is useful if you want to use sed to delete empty rows. The following regular expression can be used to match empty rows, even if it contains spaces:
^ □* $

References: http://www.linuxawk.com/communication/441.html

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.