Linux Text Processing tool sed

Source: Internet
Author: User
Tags first string

Text Processing sed!!!

Sed is a row editor for modifying the contents of a file

Function: Used to process editing one or more files

Official website: http://www.gnu.org/software/sed/manual/sed.html

By default, SED reads the data read into the pattern space automatically when each line is read, and after processing is completed, it will then automatically read the next line, starting the next cycle, and if there are no special instructions like ' D ', it will empty the pattern space between the two loops, meaning that it will clear the previous line when it is read into the next line. If there is an instruction like ' D ', the previous row of data will not be emptied, and the file contents will not be modified by default, unless some instructions are added


Usage:

sed [option] ... ' script (meaning sed command comes with the language) ' Inputfile (the file to be processed can be written multiple) ...

Format of script:

Consists of ' address commands ', which represent some lines in the file to be processed, while commands represent some of the instructions in the SED


Common options:

-N: does not output mode space content to the screen, that is, does not print automatically

-e: Multi-point editing

-f:/path/script_file: Reading the edit script from the specified file

-R: Supports the use of extended regular expressions

-i.bak: Adding a suffix will first make a backup file and then start editing the file

(Example: Sed-i.bak ' 7a alias cdnet= "Cd/etc/sysconfig/network-scripts" ~/.BASHRC first back up the ~/.BASHRC file as a ~/.bash.bak file and then in ~/. Add an alias below line 7th of BASHRC)


Address delimitation:

(1) Not to address: The full text of the processing

(2) Single address:

#: Directly to a line number, you can directly display a line (example: sed-n ' 2p '/etc/issue only print the second line of/etc/issue, p is the meaning of printing)

$: Represents the last line (example: Sed-n ' $p '/etc/fstab, print only the last line)

Mode: two//Between, can write regular expression, (example: Sed-n '/^u/p '/etc/fstab, only the lines beginning with U print out, p is the meaning of printing)

(3) Address range:

#,#: Line range, from line to line (example: Sed-n ' 9,12p '/etc/fstab, prints only data between lines 9th through 12th in the file)

#,+#: The number of lines after the first line to this line (example: Sed-n ' 9,+3p '/etc/fstab. Print only the data after line 9th through 9th of the file, that is, rows between 9 and 9+3)

/part1/,/part2/: Two lines between modes (example: Sed-n '/^b/,/^l/p '/etc/passwd show only lines beginning with B to L);

Note, however, that if there is a value that matches the first pattern after matching to the end position, it will continue to be displayed, and then the second pattern will be found to match the row, if not all will be displayed

#,/part1/: Indicates from the first line to the pattern (example: Sed-n ' 3,/^f/p ', only the line from the third line to the beginning of F, the pattern and the # can be written in reverse, for example:

Sed-n '/^f/,20p ', prints only the data between lines beginning with F to line 20th)

(4) ~ Step: (that is, increment from start position increments a few lines at a time, until the end)

Odd number of rows, 1-3-5-7-9 ...

(Example: Sed-n ' 1~2p '/etc/passwd, printing only starts from the first line each increment two lines that is the first row, then the first row +2=3, the third row +2 equals 5, and so on until the end)

2~2 even lines, 2-4-6-8-10 ....

(Example: Sed-n ' 2~2 '/etc/passwd, printing only starts from the second line each increment two lines is the second row, then the second row +2=4, the fourth row +2 equals 6, and so on until the end)


Edit command:

P: Print with front address bound

D: Delete a row of pattern space matching, by default will read each row into the pattern space, and start the next cycle, reading into a new line

(Example: cat-n/etc/passwd | sed ' 20,30d ' shows rows after deletion of 20-30, because SED automatically prints the data of the mode space, if plus-n is not displayed)

A[\]text: If the address satisfies the condition then appends the text under the line that matches the address, directly after a input to append the data to be possible, may also enter a in front of a \ meaning is that the text after the \ means is to append the text, applies to appends the space, if wants to add many lines, needs to wrap the words, Enter \ n in the position where you want to break the line

I[\]text: If the address satisfies the condition then appends the text on the line that matches the address, directly after I input to append the data to be possible, may also enter in front of the I to have a \, the meaning is that the text after the \ is to append the text, applies to to append the space, if wants to add many lines, needs to wrap, Enter \ n in the position where you want to break the line

C[\]text: Replace rows that meet previous addresses

W "Path file": Save the conforming line to a new file (example: sed '/^alias/w/app/f1.txt ' ~/.BASHRC, save the ~/.BASHRC file with alias in the/app/f1.txt file and print on the screen)

R "Path file": reads the data from the path file to the address corresponding to the file (example: sed '/^alias/r/etc/centos-release ' ~/.BASHRC, the data in the/etc/centos-release is read to the ~/. BASHRC below the line that begins with alias)

=: Displays the line number of the qualifying line (example: sed '/^alias/= ' ~/.bashrc, which displays the line number of the line starting with alias in ~/.BASHRC)

!: Inverse (example: Sed-n '/^alias/!p ' ~/.BASHRC except lines that start with alias are printed)


Very practical technology!!! (Knocking on the blackboard will be!) )

s///: Search alternative, support using other separators, [email protected]@@,s### ... If this is the default, only the first string found in this line is replaced

Replace tag:

G: Replace all matches with all

P: Show the rows that were successfully replaced

W: the same as the above meaning

Example:

Sed ' s#/bin/bash$#/sbin/nologin# '/etc/passwd, replacing all lines in the/etc/passwd file with the end of/bin/bash as/sbin/nologin

Sed-r ' s# (/bin/bash$) #------\1=====# '/etc/passwd,/etc/passwd file in front and back of/bin/bash plus---and = = = Characters


Linux Text Processing tool sed

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.