SED command for Linux

Source: Internet
Author: User

First, the initial knowledge of SED

In the process of deploying OpenStack, you will have access to a large number of SED commands, such as

# Bind MySQL service to all network interfaces.
Sed-i ' s/127.0.0.1/0.0.0.0/g '/etc/mysql/my.cnf

So what does this order mean? The next introduction to the SED command answer is naturally revealed.
Ii. introduction of the SED

Sed: is an editor and is a powerful file processing tool.

sed function: Used to replace, delete, update the contents of the file. SED can automatically process one or more files.

Sed principle: sed is processed in the Act unit of text, processing one line at a time. First, the SED stores the currently processed rows in a temporary buffer (known as pattern space), then processes the rows in the buffer, and after processing completes, the contents of the buffer are sent to the screen. The SED finishes processing a line and deletes it from the temporary buffer, then reads the next line, processes it, and displays it repeatedly until the end of the file. After the last line of the file has been processed, sed finishes running.

Because SED is editing a copy of each line in the file in the temporary buffer, the original file contents are not changed unless the output is redirected.
Iii. Introduction to the SED command

#sed [-nefri][Command]

Parameter description:

-I: Directly modify the file, the terminal does not output results.

-N: Use Quiet (slient) mode to cancel the default output. The SED defaults to output all data from stdin to the terminal. But if you add the-n parameter, do not automatically print the results after processing, only silently processing, only through the SED special processing of the line is listed.

-E:--expression is an action editor for SED directly in command mode. Sed-e ' ... '--e ' ... '

-F: Specifies the file name of the SED script.

The-r:sed action supports the syntax of the extended formal notation. (The default is the basic formal French notation)

Command description: [N1][N2] Command

N1,N2: Represents the line number, which is optional and generally represents the number of rows you want to manipulate, either a number, a regular expression, or a mixture of both.

The number of two rows separated by commas represents the range of rows from which the two behaviors start and end. The dollar sign ($) represents the last line, as 1,3 represents a one-line. If no address is specified, SED will process all lines of the input file. The usual wording of the address is: n;n,m;n,$. For example, if my operation needs to be between 3 and 5 rows, then "3,5,[action Behavior".

Command:

A: New, append a line of text to the next line of the current row.

I: INSERT, insert a line of text on the top line of the current line.

C: Replace, replace with the behavior unit, C can follow the string, use these strings to replace the line between n1,n2.

D: Delete, delete one row from the pattern space.

P: Print, print the line of the mode space. Usually p is used with the parameter "-n".

S: Replace, usually the S command can be paired with regular expressions! such as 1,20s/old/new/g.
Iv. Examples of

Below we take Emilia's big Big World part of the lyrics file Song.txt example, the content is as follows:
Copy Code

$ cat Song.txt
I ' m a big big girl
In a big Big world
It ' s not a big big thing if you leave me
But I did do feel
That I don't do would miss you much
Miss you much

Copy Code
1, S replacement command

Replace the first big of each line in the Song.txt file with small:

Sed ' s/big/small/' song.txt

Explanation: S: Replace command,/big/: Match big string,/small/: replace match to small.
Copy Code

$ sed ' s/big/small/' song.txt
I ' m a small big girl
In a small big world
It ' s not a small big thing if you leave me
But I did do feel
That I don't do would miss you much
Miss you much

Copy Code

Replace all of the big rows in the Song.txt file with small:

Sed ' s/big/small/g ' song.txt

Explanation: Ditto,/g means replace all matches on one line
Copy Code

$ sed ' s/big/small/g ' song.txt
I ' m a small small girl
In a small Small world
It ' s not a small small thing if you leave me
But I did do feel
That I don't do would miss you much
Miss you much

Copy Code

Note: It is also possible to use sed ' s#big#small#g ' song.txt, and the symbol immediately following the S command is considered to be the delimiter between the lookup string and the replacement string, where # is used, regardless of the string (except for line breaks, backslashes), as long as the S command is followed, is a new string separator.

Replace the 2nd big of each line in the Song.txt file with small:sed ' S/BIG/SMALL/2 ' song.txt

Explanation:/2 indicates a 2nd matching field action in the specified row
Copy Code

$ sed ' s/big/small/2 ' song.txt
I ' m a big small girl
In a big Small world
It ' s not a big small thing if you leave me
But I did do feel
That I don't do would miss you much
Miss you much

Copy Code
2,-I parameters directly modify the contents of the file

The above sed command does not change song.txt, just put the processed content output, if you want to write back the file, you can use redirection.

$ sed ' s\big\small\g ' song.txt >song.bak

Or use-I directly modify the contents of the file: Sed-i ' s\big\small\g ' song.txt
Copy Code

$ Sed-i ' s\big\small\g ' song.txt
$ cat Song.txt
I ' m a small small girl
In a small Small world
It ' s not a small small thing if you leave me
But I did do feel
That I don't do would miss you much
Miss you much

Copy Code

The [-i] parameter of SED can directly modify the contents of the file, which is very useful!

For example, if you have a 1 million-line file, add some text to line 100th. Using VIM at this time may go insane! Because the file is too big to open! But with the SED direct modification/substitution function, you do not need to open the file to complete the task. Compared with vim, sed is like magic, vim to open the file-operation file-close the file, the SED directly isolated on the file operation, very convenient.

Because the Sed-i function is powerful, can modify the original file directly, also is a dangerous action, need to use carefully.
3,-e parameter Editing command, multi-line matching

-E is an edit command that is used in cases where SED performs multiple editing tasks. All edit actions are applied to the line in the pattern buffer before the next line starts editing. Because multiple edits are performed line by row (that is, each command executes on the current line in the pattern space), the order in which the commands are edited affects the result.

If we want to replace more than one pattern at a time,

Sed "1,3s/big/small/g; 4,5s/do/don ' t/g ' song.txt

Explanation: The first pattern: the first row to the third row of big replaced with small, the second mode: the fourth line to the fifth row of do to replace the don ' t.
Copy Code

$ sed "1,3s/big/small/g; 4,5s/do/don ' t/g ' song.txt
I ' m a small small girl
In a small Small world
It ' s not a small small thing if you leave me
But I don ' t don ' t feel
That I don ' t don ' t'll miss you much
Miss you much

Copy Code

The above command is equivalent to: Sed-e "1,3s/big/small/g"-E "4,5s/do/don ' t/g" song.txt
Copy Code

$ SED-E "1,3s/big/small/g"-E "4,5s/do/don ' t/g" song.txt
I ' m a small small girl
In a small Small world
It ' s not a small small thing if you leave me
But I don ' t don ' t feel
That I don ' t don ' t'll miss you much
Miss you much

Copy Code
4. Delete command d: delete matching rows

Command d deletes the matching input lines, sed first copies the input lines from the file into the pattern space, then executes the SED command on the line, and finally displays the contents of the pattern space on the screen. If it is command D, the input rows in the current mode space are deleted and not displayed.

Using matching to delete rows

Delete the first line in the Song.txt file that contains "Miss": sed '/miss/d ' song.txt

$ sed '/miss/d ' song.txt
I ' m a big big girl
In a big Big world
It ' s not a big big thing if you leave me
But I did do feel
That I don't do would miss you much

Here is an example of deleting rows using line numbers

Delete the 1th line in the song.txt file: sed ' 1d ' song.txt

$ sed ' 1d ' song.txt
In a big Big world
It ' s not a big big thing if you leave me
But I did do feel
That I don't do would miss you much
Miss you much

Delete song.txt files from 2 to 5 lines: sed ' 2,5d ' song.txt

$ sed ' 2,5d ' song.txt
I ' m a big big girl
Miss you much

Delete line after line 3rd in the song.txt file: sed ' 3, $d ' Song.txt

$ sed ' 3, $d ' Song.txt
I ' m a big big girl
In a big Big world

Regular matches can be used with line numbers, for example in an OpenStack deployment:

Delete the first line containing "eth2" to the last line: Sed-i '/eth2/, $d '/etc/network/interfaces
5. Insert command A to append a row after the current line

A adds new text to the current line in the file (that is, the line in the mode space is read) after.

Inserts a row after line 3rd in the Song.txt file and acts directly on song.txt:sed ' 3a aaaaaaaaaaaaaaaaaaaaaaa ' Song.txt "with spaces as separators"
Copy Code

$ Sed-i ' 3a aaaaaaaaaaaaaaaaaaaaaaa ' Song.txt
$ cat Song.txt
I ' m a big big girl
In a big Big world
It ' s not a big big thing if you leave me
Aaaaaaaaaaaaaaaaaaaaaaa
But I did do feel
That I don't do would miss you much
Miss you much

Copy Code

Insert line after match ' Miss ': sed '/miss/a aaaaaaaaaaaaaaaaaaaaaaaaaa ' song.txt ' with space as separator '
Copy Code

$ sed '/miss/a aaaaaaaaaaaaaaaaaaaaaaaaaa ' song.txt
I ' m a small small girl
In a small Small world
It ' s not a small small thing if you leave me
But I did do feel
That I don't do would miss you much
Miss you much
Aaaaaaaaaaaaaaaaaaaaaaaaaa

Copy Code

Insert one row at song.txt: sed ' $a append line ' Song.txt
Copy Code

$ sed ' $a append line ' Song.txt
I ' m a big big girl
In a big Big world
It ' s not a big big thing if you leave me
But I did do feel
That I don't do would miss you much
Miss you much
Append Line

Copy Code
6. Insert command I, insert a row before the current line

I add new text to the front of the current line in the file (that is, the row in the mode space is read).

Insert a line after line 3rd in the song.txt file: sed ' 3i#iiiiiiiiiiiiiiiiiiiiiiiiiii ' song.txt "#作为分隔符"
Copy Code

$ sed ' 3i#iiiiiiiiiiiiiiiiiiiiiiiiiii ' song.txt
I ' m a small small girl
In a small Small world
Iiiiiiiiiiiiiiiiiiiiiiiiiii
It ' s not a small small thing if you leave me
But I did do feel
That I don't do would miss you much
Miss you much

Copy Code

Insert row before line matching ' Miss ' in song.txt file: sed '/miss/i iiiiiiiiiiiiiii ' song.txt ' space as delimiter '
Copy Code

$ sed '/miss/i iiiiiiiiiiiiiii ' song.txt
I ' m a small small girl
In a small Small world
It ' s not a small small thing if you leave me
But I did do feel
That I don't do would miss you much
Iiiiiiiiiiiiiii
Miss you much

Copy Code
7. Add more lines (I insert before line, add after line A, other places, take a as an example)

Insert the same row and insert a row after line 2nd through 5th: sed ' 2,5a append one line ' Song.txt
Copy Code

$ sed ' 2,5a append one line ' Song.txt
I ' m a big big girl
In a big Big world
Append one line
It ' s not a big big thing if you leave me
Append one line
But I did do feel
Append one line
That I don't do would miss you much
Append one line
Miss you much

Copy Code

Insert a different line to \ n Wrap

Insert three lines after line 2nd: SED ' 2a append one line \nappend, \nappend three line ' Song.txt
Copy Code

[Email protected]:~/mytest$ sed ' 2a append one line \nappend, line \nappend three line ' Song.txt
I ' m a big big girl
In a big Big world
Append one line
Append-Line
Append three Line
It ' s not a big big thing if you leave me
But I did do feel
That I don't do would miss you much
Miss you much

Copy Code
8. C command to replace with the behavior unit

Replace the contents of lines 2nd through 5th with 2-5content:sed ' 2,5c 2-5content ' song.txt

$ sed ' 2,5c 2-5content ' song.txt
I ' m a big big girl
2-5content
Miss you much

9, p command display the contents of the mode space

displayed in the Behavior unit,

Displays lines 2nd through 5th in the Song.txt file: Sed-n ' 2,5p ' song.txt

$ Sed-n ' 2,5p ' song.txt
In a big Big world
It ' s not a big big thing if you leave me
But I did do feel
That I don't do would miss you much

Search Match display:

Displays the line matching ' Miss ': Sed-n '/miss/p ' song.txt

$ Sed-n '/miss/p ' song.txt
Miss you much

10.-N parameter cancels the default output

When the P command above is displayed, the-n parameter is used because the SED defaults to printing the input line on the screen: sed '/miss/p ' song.txt
Copy Code

[Email protected]:~/mytest$ sed '/miss/p ' song.txt
I ' m a big big girl
In a big Big world
It ' s not a big big thing if you leave me
But I did do feel
That I don't do would miss you much
Miss you much
Miss you much

Copy Code

So to print the selection, use-N with P.
V. Simple regular Expressions

^: Beginning of Line locator.

$: End-of-line locator.

\<: Word header locator.

\>: Suffix locator.

.: Matches a single character except for a newline.

*: Matches 0 or more leading characters.

[]: matches any character in the character set.

[^]: matches any character not in the specified set of characters.

SED command for Linux

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.