Sed command details

Source: Internet
Author: User

Sed command details
Overview

Sed is short for stream editor, that is, stream editor. It processes a row of content at a time. During Processing, it stores the currently processed row in the temporary buffer, called the pattern space, and then uses the sed command to process the content in the buffer, after processing, the buffer content is sent to the screen. Next, process the next row, and repeat until the end of the file. The file content is not changed unless you use the redirection storage output.

 

Use syntax

The rules for using the sed command are as follows:

  1. sed[option]'command' input_file

Option is optional. The following options are commonly used:

  • -N uses silent mode (cannot figure out why it is not-s ). In general sed usage, all content from stdin is usually listed on the screen. However, if the-n parameter is added, only the row (or action) that has been specially processed by sed will be listed;
  • -E directly edits the sed action in the Command column mode;
  • -F directly writes the sed action in a file,-f filenameYou can run the sed command in filename;
  • -R enables the sed command to support extended regular expressions (the default is the basic regular expression );
  • -I directly modifies the content of the file to be read, rather than the screen output.

Common Commands include the following:

  • A \: append (append), followed by string s (multiple strings can be separated by \ n ), the string s is added to the end of the selected row;

  • C \: replace/Replace the row (change). c \ follows the string s (multiple strings can be separated by \ n), and the selected row is replaced with the string s;
  • I \: insert (insert), followed by string s (multiple strings can be separated by \ n), the string s will be inserted before the selected row;
  • D: delete. This command deletes the currently selected row;
  • P: print. This command prints the selected row to the screen;
  • S: Replace the string (subs). Generally, the usage of the s command is as follows:1,2s/old/new/gTo replace the old string with the new string.

 

Command example

Assume that there is a file test.txt with the following content:

  1. [qifuguang@winwill~]$ cattest.txt
  2. thisis first line
  3. thisis second line
  4. thisis third line
  5. thisis fourth line
  6. this fifth line
  7. happy everyday
  8. end

This section describes how to use each command in detail.

 

A command (append line)

 

Example 1
  1. [qifuguang@winwill~]$ sed'1a \add one'test.txt
  2. thisis first line
  3. add one
  4. thisis second line
  5. thisis third line
  6. thisis fourth line
  7. thisis fifth line
  8. happy everyday
  9. end

In the command section of this example, 1 indicates the first line, the same second line is written as 2, and the first line to the third line is written1,3, Use$Indicates the last line, such2,$Indicates all rows from the second row to the last row (including the second row and the last row ).

The purpose of this example is to add the "add one" string after the first line, and you can see the specific effect from the output.

 

Example 2
  1. [qifuguang@winwill~]$ sed'1,$a \add one'test.txt
  2. thisis first line
  3. add one
  4. thisis second line
  5. add one
  6. thisis third line
  7. add one
  8. thisis fourth line
  9. add one
  10. thisis fifth line
  11. add one
  12. happy everyday
  13. add one
  14. end
  15. add one

In this example, the "add one" string is added after all the rows in the first and last rows. The output shows the effect.

 

Example 3
  1. [qifuguang@winwill~]$ sed'/first/a \add one'test.txt
  2. thisis first line
  3. add one
  4. thisis second line
  5. thisis third line
  6. thisis fourth line
  7. thisis fifth line
  8. happy everyday
  9. end

In this example, the string "add one" is added to the row containing the "first" string. The output shows that the first row contains the first, therefore, "add one" is added after the first line"

 

Example 4
  1. [qifuguang@winwill~]$ sed'/^ha.*day$/a \add one'test.txt
  2. thisis first line
  3. thisis second line
  4. thisis third line
  5. thisis fourth line
  6. thisis fifth line
  7. happy everyday
  8. add one
  9. end

In this example, the regular expression is used to match rows,^ha.*day$Indicates the row starting with ha and ending with day. The "happy everyday" of the file can be matched. Therefore, the "add one" string is added after the row.

 

I command (insert line)

The method of using the I command is the same as that of the command, except that a string is inserted before the matched line. Therefore, replace a in the example of the command with I, I won't be embarrassed here.

 

C command (replace line)

 

Example 5
  1. [qifuguang@winwill~]$ sed'$c \add one'test.txt
  2. thisis first line
  3. thisis second line
  4. thisis third line
  5. thisis fourth line
  6. thisis fifth line
  7. happy everyday
  8. add one

In this example, replace the last line with the string "add one". The output shows the effect.

 

Example 6
  1. [qifuguang@winwill~]$ sed'4,$c \add one'test.txt
  2. thisis first line
  3. thisis second line
  4. thisis third line
  5. add one

In this example, replace the content from the fourth row to the last row with the string "add one ".

 

Example 7
  1. [qifuguang@winwill~]$ sed'/^ha.*day$/c \replace line'test.txt
  2. thisis first line
  3. thisis second line
  4. thisis third line
  5. thisis fourth line
  6. thisis fifth line
  7. replace line
  8. end

In this example, replace the rows starting with ha and ending with day with "replace line ".

 

D command (Delete line)

 

Example 8
  1. [qifuguang@winwill~]$ sed'/^ha.*day$/d'test.txt
  2. thisis first line
  3. thisis second line
  4. thisis third line
  5. thisis fourth line
  6. thisis fifth line
  7. end

In this example, rows starting with "ha" and ending with "day" are deleted.

 

Example 9
  1. [qifuguang@winwill~]$ sed'4,$d'test.txt
  2. thisis first line
  3. thisis second line
  4. thisis third line

In this example, content from the fourth row to the last row is deleted.

 

P command (print line)

 

Example 10
  1. [qifuguang@winwill~]$ sed-n '4,$p'test.txt
  2. thisis fourth line
  3. thisis fifth line
  4. happy everyday
  5. end

In this example, the content from the fourth line to the last line is printed on the screen. The p command is generally used together with the-n option.

 

Example 11
  1. [qifuguang@winwill~]$ sed-n '/^ha.*day$/p'test.txt
  2. happy everyday

In this example, the rows starting with ha and ending with day are printed.

 

S command (replace string)

In actual use, the s command is the most commonly used.

 

Example 12
  1. [qifuguang@winwill~]$ sed's/line/text/g'test.txt
  2. thisis first text
  3. thisis second text
  4. thisis third text
  5. thisis fourth text
  6. thisis fifth text
  7. happy everyday
  8. end

In this example, all lines in the file are replaced with text, and g indicates global, that is, global replacement. If g is not added, only the first line of the row is replaced.

 

Example 13
  1. [qifuguang@winwill~]$ sed'/^ha.*day$/s/happy/very happy/g'test.txt
  2. thisis first line
  3. thisis second line
  4. thisis third line
  5. thisis fourth line
  6. thisis fifth line
  7. very happy everyday
  8. end

In this example, match the rows starting with ha and ending with day. In this example, the matched rows are "happy everyday", and then replace "happy" in the row with "very happy.

 

Example 14
  1. [qifuguang@winwill~]$ sed's/\(.*\)line$/\1/g'test.txt
  2. thisis first
  3. thisis second
  4. thisis third
  5. thisis fourth
  6. thisis fifth
  7. happy everyday
  8. end

This example is a bit complicated. Let's break it down first. First, the s command mode iss/old/new/gSo the old part of this example is\(.*\)line$, Sed command to use\(\)The content of the package indicates the nth part of the regular expression. The sequence number starts from 1. In this example, there is only one\(\)So\(.*\)Indicates the first part of the regular expression. This part matches any string, so\(.*\)line$Match any row ending with line. Then replace the matched rows with the first part of the regular expression (in this example, the line part is deleted ).\1Indicates the first part of the match.\2Indicates the second part,\3Indicates the third part, which can be referenced in sequence. For example:

  1. [qifuguang@winwill~]$ sed's/\(.*\)is\(.*\)line/\1\2/g'test.txt
  2. this first
  3. this second
  4. this third
  5. this fourth
  6. this fifth
  7. happy everyday
  8. end

Regular ExpressionisThe two parts can be used.\1And\2Indicates that the function of this example is to delete the is in the middle.

Introduce shell variable in sed command

Shell programming in Linux -- basic usage of sed command

Sed for Unix text processing tools

Sed advanced usage

Sed command details and examples

Linux Regular Expression sed details

Sed in Linux text processing tool

This article permanently updates the link address:

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.