Linux-shell script command: Introduction to SED command

Source: Internet
Author: User
Tags command line range first row

[SED Introduction:]

Sed is a good file processing tool, which itself is a pipe command, with the Behavior unit processing, can be used to add, select, replace, delete data rows and other operations.

SED command line format: sed [-nefri] ' range command ' file

such as: sed ' 2d ' aaa.txt # Delete file Aaa.txt second line

[sed workflow:]

When using Vim as a screen editor to edit a file, we need to open this file, there are two questions:

1. Opening a relatively large file consumes a lot of memory.

2. We cannot write a script to invoke vim to edit the file, but sed can edit the file by writing a script.

Sed belongs to the stream editor, when editing a file, it first reads a line of the file into memory and reads it into the memory part, called the mode space; And then edit it according to our needs,

After editing, the contents of the mode space are output to the screen, and the contents of it are emptied, then the next line to the mode space is read, so that the whole file is avoided.

[SED common options:]

-N: Only the content in the mode space is displayed, not the content that is not edited.

-E: SED action edit directly on instruction column mode;

-F: Directly write the action of SED in a file, the-f filename can perform the SED action in filename;

-r:sed's actions support the syntax of extended formal notation. (Presupposition is the basic formal representation of French law)

-I: Directly modify the contents of the read file, not the screen output.

[sed frequently used commands:]

D: Delete, because it is deleted, so d usually do not pick up anything at the back;

S: substitution, which can be directly substituted for the work, usually this s action can be paired with the formal notation;

A: Append, a can be followed by a string, and these strings will appear on a new line (the current next line);

I: Insert, I can be followed by strings, and these strings will appear on a new line (currently the previous line);

C: Replace, C can be followed by strings, these strings can replace a line of content;

[Examples of commonly used commands:]

# # Delete Operations D:--------------------------------------------------------------- - - - - - - - - - - - - - - - - - - - -

Range is a range, and there are two ways in which a delete operation is represented:

① writes a number directly to indicate how many rows are being manipulated. Sed ' 2d ' aaa.txt #表示删除文件aaa the second line in. txt

② using regular expressions, you must use two backslashes//partitions at this point. Sed '/^tom/d ' aaa.txt # means to delete the line that starts with Tom in the Aaa.txt

* If no range is specified, the full text is manipulated. Sed ' d ' aaa.txt # means to delete all rows in the Aaa.txt

1. Sed ' $d ' aaa.txt # deletes the last line in Aaa.txt (operates on a single line)

2. Sed ' 7,9d ' aaa.txt # delete rows seventh through Nineth in file Aaa.txt (operations on multiple lines)

3. Sed '/aaa/,/bbb/d ' aaa.txt delete file Aaa.txt contains AAA to contain all rows between BBB (operate on multiple lines)

Back to the column page: http://www.bianceng.cnhttp://www.bianceng.cn/OS/Linux/

# # Substitution Operations S:---------------------------------------------------------- - - - - - - - - - - - - - - - - - - - -

Alternate format: sed ' range s/orig/new/sign ' file # If no range is specified, the full text will be manipulated.

1. The sed ' s/aaa/bbb/' ccc.txt will replace the first keyword AAA in the full text of Ccc.txt with a BBB because we have not specified any pattern

2. The sed ' s/aaa/bbb/g ' ccc.txt will replace all AAA in Ccc.txt's full text, because we have added sign:g

3. The sed ' s/aaa/bbb/2 ' ccc.txt will replace the second AAA in the Ccc.txt full text with BBB because we have added Sign:2

4. The sed ' 1s/aaa/bbb/g ' ccc.txt will replace all AAA in the first line of Ccc.txt full text, because we have added range:1 and sign:g

5. The example above can also be separated by other characters (only replaced): sed ' s#aaa#bbb# ' ccc.txt

6. Old position support regular, but in new location does not support regular, in addition to characters \ \&

Sed ' 1S/A/N&N/2 ' Ccc.txt # replaces the first line in the Ccc.txt full text with the second a Nan, & corresponds to a reference to front a

7. To do multiple operations on a specific range, then we have to put multiple operations in {}; Separate multiple commands.

Sed ' 1{s/a/b/g s/b/c/} ' Ccc.txt # First replaces all a in the first line of Ccc.txt Full-text with B and replaces the first keyword B with C in the first line

8. The above is all about replacing a word, if we want to replace a character, then you can use the y command

Sed ' 1y/abc/xyz/' Ccc.txt # Replaces all a in the first line of Ccc.txt with X, B replaces Y, and C replaces Z

# # Append A----------------------------------------------------------- - - - - - - - - - - - - - - - - - - - -

1. Sed '/ab/a\xxx ' ccc.txt # Inserts a line below the ccc.txt containing AB and insert a line of string xxx

2. Sed ' 1a zhang\njim ' ccc.txt # Insert Zhang under the first line of Ccc.txt and insert Jim on the next line, \ n Wrap Action

# # Insert Operation I:--------------------------------------------------------------- - - - - - - - - - - - - - - - - - - - -

1. Sed '/ab/i\xxx ' ccc.txt # Insert one line above the ccc.txt containing AB and then the string xxx

2. Sed ' $a abc ' Ccc.txt # Insert ABC in the last line of Ccc.txt

# # Replaces Operations C:-------------------------------------------------------------- - - - - - - - - - - - - - - - - - - - -

1. The sed '/ab/cab=ab ' Ccc.txt # replaces the entire row of ccc.txt containing AB as Ab=ab

2. Sed ' 1c AB ' Ccc.txt replaces the first line of the ccc.txt with the whole row of AB

# # Show Mode space P:------------------------------------------------------- - - - - - - - - - - - - - - - - - -

1. Sed ' $a ABC P ' Ccc.txt # to display both the schema space and the contents of the file on the screen

[sed other commands:]

1. = To display line numbers

Sed '/^abc/{=} ' Ccc.txt # The line number of the keyword that starts with ABC

2. N to get the next line (the current row is deleted from the mode space, the next line reads into the mode space)

The sed '/^a/{n;s/b/b/g} ' Ccc.txt # matches the next line that begins with a and replaces B with B.

3. N reads multiple-line content to the mode space: (Current and next lines are in the mode space)

Sed '/^a/{s/tb/tb/; N S/T\NB/TB \n/} ' Ccc.txt # matches rows beginning with a, replacing TB with TB; Read the next line and replace the current line of T with the next line of B to TB

4. Using multi-line mode space, ^ is not the beginning of the line, but the beginning of the pattern space; $ is not the end of the line, but the end of the pattern space.

Before replacement: A B C D e replacement: A b c D E

A b c d e a B c d E

Sed ' N; s/^a/a/' Ddd.txt # The first and second lines will be read into the pattern space, but only a in the first row will be replaced with a, and a in the second row will not be replaced.

Before replacement: A B C D e replacement: A b c D E

A b c d e a B c d E

Sed ' N; s/e$/e/' Ddd.txt # The first and second lines will be read into the pattern space, but only the second line of E will be replaced with E, and the first line of E will not be replaced.

5. In addition to the mode space, there is space to maintain, we can interpret it as a cache. The contents of the space and the mode space can be exchanged.

H: Storage of mode space content to maintain space; G: Store the contents of the space inside the mode space;

Before replacement: a replace: b

b A

AA BB

BB AA

Sed '/a/{h d}; /b/{g} ' Eee.txt # matches the line containing a, keeping them in space and removing them from the pattern space, matching the row containing B, and putting the space content into the pattern space;

6. Set there is no loop, if you want to achieve the effect of the loop, you can use the label

Author: csdn Blog zdp072

Related Article

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.