Common Linux commands: sed and common linux commands: sed

Source: Internet
Author: User

Common Linux commands: sed and common linux commands: sed

This article records notes about regular expressions in your study of Linux Private food.

About the end of a line $

If the file itself has no content, such as the file created by touch, then $ will be meaningless.For example:

Worker manually adds a line of content "add by vim", and then runs the same sed command, for example, the output result of subsequent cat commands.

Command sed

Sed is a very useful pipeline command with many functions and is used in many scenarios.

Sed [-nefri] [action]

  • -N: Quiet Mode. By default, all stdin directed to sed is displayed on the screen. If quiet mode is used, only the lines or commands that have been specially processed by sed are displayed on the screen.
  • -E: directly edit the sed action in command line mode.
  • -F: Write the sed action directly in a file.-f filename can execute the sed action in filename.
  • -R: enables sed to support expanded regular expression syntax. The default value is the basic regular expression syntax.
  • -I: directly modify the content of the file to be read, rather than output on the screen. The default output is on the screen, but the source file is not affected.

The following is an action description. The action must be enclosed in single quotation marks ''. You can use two numbers or $ to select the number of rows for the action, '[ n1 [, n2] function ', for example, '2, 5d ', '2d', '3, $ d' indicate to select rows [2-5] and 2nd rows respectively, [3-last line]. You can also use a regular expression to select rows. For example, '/regular expression/d', the row with the symbol regular expression is deleted. Function has the following operations:

  • A: New. A must be followed by a string and not all strings with spaces, indicating that the data is written to the next row of the current row.
  • C: replace. C must be followed by strings that replace the rows between n1 and n2, that is, delete the rows [n1, n2], and then write these strings after the n1-1
  • D: Delete. Delete row [n1, n2]
  • I: insert. I must be followed by strings. These strings will show the previous line of the current row.
  • P: print. Print the selected data on the screen. Usually with-n
  • S: replace. Replace directly. You can use regular expressions such as '1, 20 s/old/new/G'

Function Scope, The test shows:

  • If this parameter is not selected, for example, 'a str', for a, d, I, p, all rows are selected. For c, not all rows are deleted, but only one row of str is added. Instead, all rows are replaced with str.
  • If yes, for example, '1a str ', '1, 3a str', '2, $ a str ',' $ a str ', '/regular expression/a str'; For a, d, I, p, these rows are selected for action. For c, if the row is selected in the form of [n1 [, n2], all rows are deleted and a line of str is added. If the row is selected using a regular expression, replace each row with str!

     

  • If the file is empty, these functions will be ineffective, including the command a, I

Use a to add multiple rows: The Backslash "\" must be used after each row to add a new line. For example, test.txt contains only one row: this is a test file. Then, three rows are added using.

Use sed to delete empty rows: Sed-I '/^ $/d' filename. Explanation:-I indicates that the file is directly modified and not displayed on the screen./^ $/uses a regular expression, in the regular expression, ^ indicates the beginning of the row, $ indicates the end of the row, and if there is nothing between the end of the row and the beginning of the row, it is a blank line, and d indicates deletion.

The sed command has a search and replacement function:

Sed's/string to be replaced/New String/G' filename

The regular expression is used to replace the string. For a new string, it must be a common character. Below are some of my tests.

 

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.