Linux Common commands: SED

Source: Internet
Author: User

This article is recorded in the study of "Linux private dishes" in the expression of the notes.

About the line tail character $

If the file itself has no content, such as using touch to create a new file, then $ will be meaningless. For example, do the following:

Using touch to create a new Test.txt file, and then try to use the SED command to add content at the end of the line, but no success (the next cat command did not output anything), and then I use VIM to test.txt manually add a row of content "add by Vim", The same sed command is then used to succeed, such as subsequent cat command output results.

Command SED

Sed is a very useful pipe command with a lot of features and is used in more scenarios.

sed [-NEFRI] [action]

    • -N: Quiet mode. By default, all stdin to SED are displayed on the screen, and if quiet mode is used, only lines that have been specially processed by sed or commands are displayed on the screen
    • -E: Action edit for SED directly in command line mode
    • -F: The action of SED is written directly in a file, and-f filename can perform the SED action in filename
    • -R: Enable SED to support extended regular expression syntax, default to the underlying regular expression syntax
    • -I: Directly modifies the contents of the read file rather than outputting it on the screen. The default is to output on the screen, and the source files are not affected

Here is the action description, the action requires the use of single quotation marks, and can use two numbers or $ to select the number of lines of action, ' [n1[,n2]]function ', such as ' 2,5d ', ' 2d ', ' 3, $d ' These three forms, respectively, the choice [2-5] line, line 2nd, [3- Last line]. You can also use regular expressions to select rows, such as '/regular expressions/d ', to delete the lines of the symbolic regular expression. The function has the following actions:

    • A: added. A string that must be followed after a, and not all spaces, indicates that the data is written on the next line of the current line
    • C: Replace. C must be followed by strings, which replace rows between n1,n2, delete rows [n1,n2], and then write these strings after n1-1
    • D: Delete. Delete Row [n1,n2]
    • I: Insert. I must be followed by a string that will appear on the previous line of the current row
    • P: Print. Prints the selected data, which is displayed on the screen. Usually together with-n
    • S: Replace. You can use regular expressions, such as ' 1,20s/old/new/g ', to replace work directly

on the scope of function , the test shows that:

    • If there is no choice, i.e. ' a str ', then all rows are selected for A,d,i,p. For C, instead of deleting all rows and then adding only one row of STR, replace all rows with str
    • If there is a choice, e.g. ' 1a str ', ' 1,3a str ', ' 2, $a str ', ' $a str ', '/regular expression/a str '; for a,d,i,p, the rows are selected for action. For C, if these choices are contiguous, replace all of them with a single line of STR, and if they are separate, it is equivalent to deleting all the fragments and adding a row of str at each fragment position.
    • If the file is empty, these features will be ineffective, including adding commands to the class A,i

use A To add multiple lines : After each line you must use the backslash "\" to increase the new row, for example: Test.txt has only one line: This is a test file, and then use a to add three lines

use sed to delete empty lines : sed-i '/^$/d ' filename, explanation:-I means to directly modify the file without appearing on the screen,/^$/uses a regular expression, in the regular expression ^ means the beginning of the line, the $ means the end of the row, the end of the line and the beginning of the row is empty, and D is deleted.

The SED command has a Find and replace function:

Sed ' s/string to be replaced/new string/g ' filename

Where the "substituted string" is used as a regular expression, and for "new string" it must be a normal character. Here are some of the tests I did.

Linux Common commands: SED

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.