Linux Learning Path-sed command

Source: Internet
Author: User

sed Command

Working mechanism: Each time a line of text is read into "pattern space", the processing is done in the mode space, and the processing result is output to the standard output device;

Grammar : sed [OPTION] ... {Script} [Input-file] ...

[OPTION] Description

- R: supports extended regular expressions;

Example: Sed-rn '/(450|300)/P ' testfile

- N: silent mode, sed default output processed content, if used with the P command, only the processed rows are printed

Example: Sed-rn '/(450|300)/P ' testfile

- e script1-e script2-e script3 : Specifies multiple scripts to run,-e executes multiple commands, executes one more than one-e designation, as long as it is used in awk, ends with n commands

Example: Sed-n-E '/tom/p '-e '/dan/p ' testfile

-f/path/to/script_file : Reads the script from the specified file and runs; When the command file is introduced with-F, the order of execution is up and down, one command per line

Example: Sed-n-f/root/sedscript testfile

- I.: directly modify the source file;

Example: Sed-i-n-e '/tom/p '-e '/dan/p ' testfile

Address Delimitation :

        #: specify the line;

Example: Sed-n ' 5p ' testfile

        $: The last line;

Example: Sed-n ' $p ' testfile

/regexp/ : Any row that can be matched to by RegExp;

Example: Sed-n '/guy/p ' testfile

\%regexp% : Ibid., except for the regexp boundary symbol;

Example: Sed-n ' \%guy%p ' testfile

/regexp/| :

\%regexp%| : Ignoring character case when matching;

Startline,endline :

#,/regexp/ : Start with # lines, to the end of the first line that is matched by/regexp/, all rows in the middle. Or fully specify the front and rear line numbers #,#

Example: Sed-n ' 1,/chet/p ' testfile

sed-n ' 1,2p ' testfile

/regexp1/,/regexp2/ : Starting from the first line that was/regexp1/matched to, to the end of the line that was first matched to the/regexp2/, all the rows in the middle;

Example: Sed-n '/^a/,/^b/p ' testfile

#,+n : Starting with # lines, up to n rows down;

Example: Sed-n ' 1,+2p ' testfile

First~step : Specify the starting line, and the step size;

Example: Sed-n ' 1~2p ' testfile interlaced output

Edit command

D: Delete rows in the pattern space;

Example: sed ' 1d ' testfile

        = : Displays the line number;

Example: sed ' = ' testfile

a \text : Append text

Example: sed ' 1a text ' testfile

I \text : Insert text, support \ n implement multi-line insertion;

Example: sed ' 1i text ' testfile

C \text : Replaces the matching line with text;

Example: sed ' 1c text ' testfile

P: print the lines in the mode space;

Example: Sed-n ' 1p ' testfile

s/regexp/replacement/ : Replace the content that is matched by regexp to replacement;

Example: sed ' s/abc/efg/' testfile

s/regexp/replacement/g : global substitution;

Example: sed ' s/abc/efg/g ' testfile

W/path/to/somefile : Save the specified content to the file specified by the/path/to/somefile path;

Example: sed ' 1w/root/sedsave ' testfile

R/path/from/somefile : Inserts all the contents of another file at the specified location of the file, completing the file merge;

Example: sed ' 1r/root/awktest/t ' testfile

Some examples:

Delete: D command

$ sed ' 2, $d ' Example----- Delete the second line of the example file to the end of all rows.

$ sed ' $d ' example----- Delete the last line of the example file.

$ sed '/test/' d example----- Delete all rows that contain test in the example file.

Replace: s command

$ sed ' s/test/mytest/g ' example----- Replace test with mytest within the entire row range. If there is no G tag, only the first matching test of each row is replaced with mytest.

$ Sed-n ' s/^test/mytest/p ' example-----(-N) the option and the P flag are used together to indicate that only those rows that have replaced are printed. That is, if the test at the beginning of a line is replaced with mytest, it is printed.

$ sed ' s/^192.168.0.1/&localhost/' example-----& the symbol represents the part that is found in the replacement string. All lines starting with 192.168.0.1 will be replaced with a self-added localhost, which becomes 192.168.0.1localhost.

$ Sed-n ' s/\ (love\) able/\1rs/p ' Example----- love is marked as 1, all loveable are replaced with lovers, and the replaced rows are printed.

$ sed ' s#10#100#g ' example----- whatever the character, followed by the S command is considered a new delimiter, so, "#" Here is the delimiter, instead of the default "/" delimiter. means to replace all 10 with 100.

Range of selected rows:

$ Sed-n '/test/,/check/p ' Example----- all lines that are within the range determined by the template test and check are printed.

$ Sed-n ' 5,/^test/p ' Example----- prints all rows starting from line fifth to the first one that contains the line starting with test.

$ sed '/test/,/check/s/$/sed test/' example----- for the row between the template test and West, the end of each line is replaced with the string sed test.

Multi-point editing: E command

$ Sed-e ' 1,5d '-e ' s/test/check/' example-----(-e) option allows multiple commands to be executed in the same row. As shown in the example, the first command deletes rows 1 through 5, and the second command replaces test with a check. The order in which the commands are executed has an effect on the results. If all two commands are replacement commands, the first substitution command affects the result of the second replacement command.

$ sed--expression= ' s/test/check/'--expression= '/love/d ' Example----- a better command than-E is--expression. It can assign values to an SED expression.

Read from File: R command

$ sed '/test/r file ' example-----file The contents of the file are read in, after the line that matches the test, and if more than a row is matched, the contents of the text are displayed below all matching rows.

Write file: w command

$ Sed-n '/test/w file ' example----- all rows containing test in example are written to file.

Append command: A command

Next: N command

$ sed '/test/{n; s/aa/bb/;} ' Example----- if test is matched, move to the next line of the matching line, replace the AA for this line, change to BB, and print the line, and then continue.

Deform: Y command

$ sed ' 1,10y/abcde/abcde/' example----- Turn all ABCDE in 1--10 into uppercase, note that regular expression metacharacters cannot use this command.

Exit: Q command

$ sed ' 10q ' example----- After you finish printing line 10th, exit sed.

This article is from the "naïve Little Comrade" blog, please be sure to keep this source http://dengxi.blog.51cto.com/4804263/1697150

Linux Learning Path-sed command

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.