Linux sed command

Source: Internet
Author: User

Introduction to Commands

Basic command format

sed [Common options] Command text input

Common options

-N (--quiet,--silent): Quiet mode. In the basic usage of SED, all information from the standard output is listed on the terminal. With the-n parameter, only those rows processed by the SED will be output.
-e: Specifies the command text to execute on the instruction-column mode. You do not need to specify it by default, you need to explicitly specify the-e option only if you want to execute multiple command literals at the same time.
-F: When executing multiple command literals, you can write the command text to a file and then use the-f filename.
The-r:sed default uses the underlying regular expression syntax (BRE), which specifies the-r option after using the extended regular expression syntax (ERE).
-I: Directly modifies the read document, not the output to the terminal.

Common commands

A: New lines, a after the string, these strings will be added below the matching line.
C: Replace lines, followed by a string of C, that will replace the matching rows.
D: Delete rows, delete rows to match.
I: Insert the line, the following string of I, and these strings will be inserted above the matching line.
P: Print, output some lines. Normally p is used with parameter-n so that only the matching rows are output.
S: string substitution, which is used primarily with regular expressions.

Explain the difference between "command" and "command text" in this article:
Commands are abstract operations, such as a indicates new rows, and D indicates that rows are deleted.
The command text is a string that is combined by a command and some other information to perform the specific operation.
For example, add a line below the first line, the content is ' Hello World ', the command text is: ' 1a Hello World '
If you delete the line containing the string ' Hello world ', the command text is: '/hello world/d '

General Options and commands

Description: In this example, the demo file Test.txt contains three lines of text, as follows:

Aabbcc

The demo file Hello.txt contains three lines of text with the following contents:

Hello world! Hello jack! Hello china! Hello nick!
Delete Row

To delete a row you need to use command d:

$ sed ' 1d ' test.txt # Delete first line $ sed ' $d ' test.txt # Delete last line $ sed ' 1,2d ' test.txt # Delete first line to second row $ s Ed ' 2, $d ' Test.txt # Delete the second line to the last row

Note that after executing the above command, we can only see the correct results on the command line terminal, and the Test.txt file is not changed at all:

650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/952033/201707/952033-20170708213349565-278317235. PNG "style=" margin:0px;padding:0px;border:0px; "/>

Option-I.

If you want to modify the original file directly (in fact, first to modify the contents of the file and then save to the original file), you need to use the option-I:

$ Sed-i ' 1d ' test.txt

650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/952033/201707/952033-20170708213447144-654597790. PNG "style=" margin:0px;padding:0px;border:0px; "/>

Note that there is no output on the command line after the-I option is applied, but the source file is updated.

New row

The a command can add a row below the matching line:

$ sed  ' 1a hello world! '  test.txt                   #  Add a line below the first line with the content   "hello world!" $ sed  ' $a  hello world! '  test.txt                   #  Add a line below the last line, with the content   "hello world!" $ sed  ' 1,3a hello world! '  test.txt                 #  Add a row below the first row, the second row, and the third row, respectively, content                                                        #  for   "hello world!"  1,3  represents $ sed  ' 1A&NB from the first row to the third rowSp hello world!\nhello china! '  test.txt    #  add multiple lines at once you need to use line break  \n

650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/952033/201707/952033-20170708213557597-1975072486. PNG "style=" margin:0px;padding:0px;border:0px; "/>

Option-E

The-e option is used to specify the command text, and the-e option can be omitted if there is only one command text. How to specify multiple command literals requires the-e option.

$ Sed-e ' 1a xxx '-e ' 2a yyy ' test.txt
Insert Row

The I command inserts a row above the matching line, and the syntax is the same as the new row, except for the line above the specified line (the difference from the A command):

650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/952033/201707/952033-20170708213723644-829744359. PNG "style=" margin:0px;padding:0px;border:0px; "/>

Option-F

We have added more command text with option-e earlier, but if you need to add more command text, using option-e is not appropriate. Because writing all of the command text on the command line can cause maintenance difficulties. At this point, the option-F comes in handy. We can write multiple command text to a text file and then reference it with the-f option.
We create a file called commands, which adds three command text as follows:

1i Hello world!2i Hello world!3i Hello world!

Then execute the command:

$ sed-f Commands Test.txt

650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/952033/201707/952033-20170708213913550-1845418203. PNG "style=" margin:0px;padding:0px;border:0px; "/>

With the-f option, the three command text in the commands file is executed!

Replace Line

The C command makes it easy to replace an entire line:

$ sed ' 1c Hello world! ' Test.txt # Replace the first line with "Hello world!" $ sed ' 1,3c Hello world! ' test.txt # Replace the first line to the third line with "Hello world!"

650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/952033/201707/952033-20170708214054628-1221142441. PNG "style=" margin:0px;padding:0px;border:0px; "/>

Notice that the command in is replacing three lines of text with a line of text!

String substitution

Unlike row substitution, the S command replaces only the matching content (typically a string):

$ sed ' s/hello/hi/' hello.txt # replace Hello with Hi

650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/952033/201707/952033-20170708214233456-1765315747. PNG "style=" margin:0px;padding:0px;border:0px; "/>

One of the puzzles that brings us is: Why is only the first Hello in the first row replaced? The answer is that SED will only replace the first match to the default! So here's our second puzzle: If you just replace the first match, why is the second and third row of Hello replaced? The problem involves the way sed works, and sed is a tool for text processing in behavioral units! So the three lines in the figure are divided into three times, one line at a time. So Hello in the second and third rows is the first match to the bank and is replaced with the correct one.
To make a global substitution, you need to specify G in the command text and try the following command:

$ sed ' s/hello/hi/g ' hello.txt # Replace all Hello matches to Hi

650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/952033/201707/952033-20170708214333519-131824033. PNG "style=" margin:0px;padding:0px;border:0px; "/>

The two hello in the first line is replaced.

We can also restrict the lines that perform the substitution operation:

$ sed ' 2,3s/hello/hi/g ' hello.txt # Replace operation only on second and third lines

Of course, you can also remove unwanted strings by replacing them:

$ sed ' s/hello//g ' hello.txt # Delete string Hello

Delimiter

Although/is the most commonly used delimiter, you can use other characters as well. As a simple example, when you want to replace a path under Linux, it is very uncomfortable to use/as a delimiter (a lot of escape characters are required), and a delimiter is the best solution:

650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/952033/201707/952033-20170708214720862-2036540023. PNG "style=" margin:0px;padding:0px;border:0px; "/>

We used the semicolon as a delimiter to easily implement the path substitution.

The

The attentive classmate may have noticed that all of the SED operations are built on top of the row. That is to say, whatever you want to do, first find (match) the target line. Even the simplest delete line ' 1d ' must first be positioned to the first row before it can be deleted. So the only thing that can limit our ability to play sed is: How to match the desired line?

The answer is to master the basic rules and then practice more! The-n option and the P command are good helpers for our practice. The-n option tells Sed to output only those rows that have been processed. For example, the SED ' 1a Hello world! ' test.txt command outputs four rows by default, and only outputs one line after applying-N:

650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/952033/201707/952033-20170708214738628-818475303. PNG "style=" margin:0px;padding:0px;border:0px; "/>

The p command tells SED to output only those rows that match, such as the command:

$ Sed-n ' 1p ' test.txt and command sed-n ' 2,3S/HELLO/HI/GP ' hello.txt

650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/952033/201707/952033-20170708214824315-832061014. PNG "style=" margin:0px;padding:0px;border:0px; "/>

There are probably two types of rules for row matching: matching by line number and matching by regular expression.

Here are some examples of matching by line number:

$ Sed-n ' 1p ' Test.txt # matches the first line $ Sed-n ' $p ' Test.txt # matches last line $ Sed-n ' 2,3p ' test.txt # match the second and third lines $ Sed-n ' 3, $p ' Test.txt # matches each row after the third and third rows

The following are examples of matching with regular expressions:

$ Sed-n '/hello/p ' hello.txt

The default match is case-sensitive and you can use I (Capital letter i) to ignore case

$ Sed-n '/HELLO/IP ' hello.txt

The following are examples of actions performed after a regular expression match:

$ sed '/hello/d ' hello.txt # Find a matching line and remove the sed '/hello/a world! ' hello.txt # Find matching lines and add new lines below them $ sed ' /hello/s/world/china/g ' Hello.txt # finds matching rows, performs replacements in these rows


Linux 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.