Linux sed command

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. /    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, and C can be followed by a string, replacing the line between n1,n2 with these strings.

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:

$ 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
1. sReplace 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.

$ sed 's/big/small/' song.txtismall big girlsmall big worlditSmall big thing if you leave me butI don't do feelwould miss you muchmiss you much

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

$ sed 's/big/small/g' song.txtiSmall small girlSmall Small worldit  Small Small do do does do would miss you muchmiss you much 

Note: You can also use sed ' s#Big#Small#g ' Song.txt, The symbol that follows 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 by a new string delimiter.

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

$ sed 's/big/small/2' song.txt Ismall girlsmall worldit Small thing if you leave mebut i  do doing feelthat I do do would miss you Muchmiss
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

-I.
$ 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

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. - eParameter editing command for 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.

$ sed "1,3s/big/small/g; 4,5s/do/don ' t/g ' song.txt
Small Small girl
Small Small world
Small Small thing if you leave me
don ' t don ' t feel
don ' t don ' t miss you much
Miss you much

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

- e - e "4,5s/do/don ' t/g" song.txt
Small Small girl
Small Small world
Small Small thing if you leave me
don ' t don ' t feel
don ' t don ' t miss you much
Miss you much
4. Delete command D: Delete a matching row

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 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 ina big big worldit doing do feelthat i'll miss you much

Here is an example of deleting rows using line numbers

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

$ sed ' 1d
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 girlmiss you much

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

$ sed ' 3,$d
I ' m a big big girl
In a big Big world
5. Insert command a, in the currentafter the lineAppend a row

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"

$ Sed-i ' 3a
$ 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

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

$ 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

In Song.txt last insert line: sed '$A append lines ' Song.txt

$ sed '$a append line ' song.txt i' m a big big girl ina big big worlditdoing dofeelthat I do D o 'll Miss Muchmiss you muchappend  Line
6. Insert Command I, in the currentbefore the lineInsert a row

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 "#作为分隔符"

sed ' 3i#iiiiiiiiiiiiiiiiiiiiiiiiiii ' song.txt                I' m a small small girl ina small Small WorldiiiiiiiiiiiiiiiiiiiiiiiiiiiIt does do Would Miss Muchmiss you much

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

$ sed '/miss/i iiiiiiiiiiiiiii ' song.txt                           i' m a small small girl ina small small worldit' s n OT a small small thing if you leave mebut I  do does do would miss you much IIIIIIIIIIIIII IMiss you much
7, add more lines ( IInsert before line, aAfter the line is appended, as elsewhere, to aAs an example)

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

$ sed ' 2,5a append one line ' song.txt I' m a big big girl ina big Big Worldappend one line It's not a big big thing if you leave meappend one line does do feelappendone line C9>do do'll miss you muchappend  one line miss you much

Insert a different line to \ n Wrap

Insert three lines after line 2nd: sed ' 2a append one line \ nappend line \ nappend three lines ' Song.txt

[Email protected]:~/mytest$ sed ' 2a\n\append three line '   song.txt I' m a big Big girl ina big Big Worldappendone line append, line append three line Itdodo
     'll Miss you muchmiss much
8. CCommands are replaced by behavior units

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 girl2-5contentMiss you much
9. Pcommand to display the contents of a 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' ina big big worldit's nota big big thing if you leave mebut I did do feelthat I do D O'll miss you much

Search Match display:

Displays the line matching ' Miss ': sed- n '/miss/P' song.txt

$ Sed-n '/miss/P'Miss you much
10. - NParameters Cancel 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

[Email protected]:~/mytest$ sed '/miss/p ' song.txt    I' m a big big Girlin a big big worldit ' s not A big big thing if you leave mebut I do does feelthat I do do would miss you muchmiss you much miss you much   

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.

For more in-depth information, refer to the following:

"Shell Basics 12"

SED concise tutorial-cool shell Chenhao

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.