SED command usage

Source: Internet
Author: User
Tags first string

Note: The three large text lookup tools for Linux: grep, sed, awk.

SED command basic usage:

Stream Editor: line editors; implements progressive text, while full screen editor is vim.

Operating characteristics:

Instead of dealing with the text file itself, rather than processing the contents of the file, it is progressive to load the text into the memory space, complete processing in memory, and then output to the screen, in the case of SED, the memory space is called the pattern space, the lines of text can be pattern-matched before processing, SED has many editing commands.

The SED command does not edit the original file by default, only handles the data in the pattern space, and then, after processing finishes, displays the contents of the schema space to the screen.

SED command format

sed [options] ' Addresscommand ' FILE ...

-N: Silent mode, no longer displays the contents of the mode space by default;

-I: Directly modify the original file;

-E ' addresscommand '-e ' addresscommand ': Execute multiple editing commands simultaneously;

-f/path/to/sed_script: Save each edit command ' Addresscommand ' to a file, and read the edit command from the file for processing;

-R: Represents a regular expression using an extension;

Address: Specify the editing scope of the line, the notation is;

1, Startline,endline: Start line, end line; For example 1,100

$: The last line, that is, the countdown to the first row;

$-1: Indicates the penultimate line (is the doubt supported?) );

2,/regexp/: use regular matching; for example:/^root/

3,/pattern1/,/pattern2/: For the first time by the Pattern1 match to the beginning of the line to the first time to be matched to the end of the line; for example, sed '/oot/d '/etc/fstab means deleting rows containing oot in the file;

4, LineNumber: the specified line;

5, Startline,+n: To start from the StartLine, backward N lines; for example, sed ' 1,+2d '/etc/fstab delete the first 3 rows;

Command: Edit Commands

D: Delete the line of the symbol condition; for example, sed ' 1,2d '/etc/fstab means to delete the 1th, 2 lines of the file, and then display to the screen;

P: The line that displays the symbol condition, for example, sed '/^\//p/etc/fstab displays rows with/start, the qualifying row is displayed 2 times, because the original file is also displayed once, and the matching file is displayed again; You can use the-n option to Sed-n '/^\//p/etc/ Fstab

A \string: Appends a new line to the line followed by a string, such as sed '/^\//a \# hello word '/etc/fstab means adding 1 lines below the line with the/start # Hello Word;

If you add 2 lines, then sed '/^\//a \# hello word\n# hello word '/etc/fstab

\ n: for line break;

I \string: Add a new line before the line that matches, the content is string;

R FILENAME: Adds the contents of the specified file to the downstream of the qualifying line; For example, the sed ' 2r/etc/issue '/etc/fstab means to add the issue file below line 2nd (which can be used for merging files), or for example sed ' 1,,2r/etc/ Issue '/etc/fstab

W FILENAME: Saves the specified range of content to the specified file, for example sed-n '/oot/w/tmp/oot.txt '/etc/fstab indicates the row where the matching oot is located, and is saved as a oot.txt file;

s/pattern/string/: Find and replace, pattern can use regular, the default is to replace only the first time in each row by the pattern match to the string, for example, sed ' s/oot/oot/'/etc/fstab means to find oot replaced by oot; SED ' s/^ \//#/'/etc/fstab indicates that the line with/begins with #, and the sed ' s/\//#/'/etc/fstab means replacing only the first string in each row that is matched to the pattern;

Add modifier:

G: global substitution; for example, sed ' s/\//#/g '/etc/fstab indicates that the line with/begins with a global substitution of #;

I: ignoring character case;

s///: It also uses other separators, as long as it is not the same as the string used, such as [email protected]@@ or s###, which avoids escaping;

Reference: &: Represents the entire string that the reference pattern matches to, similar to the grouping in the regular form, for example, the contents of the Sed.txt file are hello,like. hi,my love; find sed ' s#l. E#&r#g ' sed.txt results for hello,liker.hi,my lover. or written as sed ' s#\ (L. e\) #\1r#g ' Sed.txt

For example:

History | Sed ' s#^[[:space:]]*# #g ' | Cut-d '-F1 indicates the deletion of whitespace characters at the beginning



SED command usage

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.