Linux Common Command learning sed command __linux

Source: Internet
Author: User

Introduction to SED

Sed is an online editor that handles a single line of content at a time. When processing, the currently processed rows are stored in a temporary buffer, known as pattern spaces, followed by the SED command to handle the contents of the buffer, and the contents of the buffer are sent to the screen after processing is complete. The next line is processed so that it repeats until the end of the file. The contents of the file do not change unless you use redirected storage output. SED is used to automatically edit one or more files, to simplify the repetitive operation of files, to write conversion programs, and so on.

Command format: sed-options script file or sed-options-f scriptfile file


System: Ubuntu11.10

Sed:gnu sed version 4.2.1


Options Command Item

-N Closes the contents of the automatic output mode space

-e script command to execute

-F file to execute script command files, if it is a large number of scripts, you can write them to a file and then use-F to specify the file as the command to execute

-i[suffix] Edit the file in place, and if the suffix [suffix] is specified, back up the source file

-L-N specifies the row to be edited, specifying multiple lines, such as 1,3 1 2 3 rows

--posix Disable all gun extensions

-R using extended regular expressions in script commands

-S separates multiple files that are processed simultaneously, rather than a long stream of data as a whole

-U imports minimal data from input file and refreshes output cache more frequently


Script Scripts Command

A\ add a line of text after the current line

B lable branch to the mark in the script and branch to the end of the script if the branch does not exist

C\ change the text of the line with new text

D Delete rows from the template block (pattern spaces)

D Delete the first line of a template block

I\ inserts text above the current line

H Copy the contents of the template block into an in-memory buffer

H Append the contents of the template block to the buffer in memory

G Gets the contents of the memory buffer and replaces the text in the current template block

G gets the contents of the memory buffer and appends it to the back of the current template block text

L list cannot print a list of characters

N reads the next input line and processes the new line with the next command instead of the first command

N appends the next input line to the template block and embeds a new line between the two, changing the current line number.

P Print the line of the template block

P Print the first line of a template block

Q Exit SED

R file reads rows from file

W file writes and appends template blocks to file end

W file writes and appends the first line of the template block to the end of the file.

T label if branch, starting at the last line, once the condition is met or the t,t command, it will cause branching to the numbered command, or to the end of the script

The T label Error branch, starting at the last line, causes an error or T,T command to branch to a numbered command, or to the end of the script

! Indicates that the following command has no effect on all rows not selected

S/re/string replaces the regular expression re with a string

= Print Current line number

# Extend the annotation before the next line break

The following are replacement tags:
* g means full replacement in line
* p indicates print line
* W indicates that the line is written to a file
* x represents interchange of text in template blocks and text in buffers
* y means to translate a character into another character (but not for regular expressions)


Meta Character Set reference: http://blog.csdn.net/luochuan/article/details/7346811


Example:

Adds an end to the line following the start of the Sed.ini file;

#sed-E '/^well/a\ end; '/etc/sed.ini

Delete the contents of line 7th to end of the Sed.ini file

#sed-E ' 7, $d '/etc/sed.ini

Delete all rows containing this in the Sed.ini

#sed-E '/this/d '/etc/sed.ini

Replace this with this in the Sed.ini file

#sed-E ' s/this/that/g '/etc/sed.ini

Replace the well starting line in the Sed.ini file with OK, and print only the rows that match to

#sed-N-E ' s/^well/ok/p '/etc/sed.ini

Replace the loveable in the Sed.ini file with lovers

#sed-E ' s/(Love) able/\1rs/g '/etc/sed.ini

Multi-point Edit command-E

Replace the well starting line in the Sed.ini file with OK, and add this to the back of the line

#sed-E ' s/^well/ok/g '-e '/here/a\ this '/etc/sed.ini

Reads the contents of the sed.php and displays it in Sed.ini after all lines starting with this

#sed-E '/^this/r/etc/sed.php '/etc/sed.ini

Write all the PHP-containing rows in the Sed.ini to the sed.php file

#sed-E '/php/w/etc/sed.php '/etc/sed.ini

Insert the---Before the line that contains this

#sed-E '/this/i\---'/etc/sed.ini

Place the line containing this in the Sed.ini into the buffer and delete the row, then replace the row containing that with the contents of the buffer

#sed-E '/this/{h;d} '-E '/that/{p;x} '/etc/sed.ini

Place the line containing this in the Sed.ini into the buffer and add it to the line that contains that

#sed-E '/this/{h;d} '-e '/that/g '/etc/sed.ini

Replace that in the next line of the line containing this in Sed.ini to this

#sed-E '/this/{n;s/that/this/}/etc/php.ini

Print the contents of Sed.ini until line 5th and exit

#sed-E ' 5q '/etc/sed.ini

Print Sed.ini and display line numbers

#sed-E ' = '/etc/sed.ini

Replace the line containing this in the Sed.ini file with the new words

#sed-E '/this/c\the new words '/etc/sed.ini

Print the second line of the Sed.ini file

#sed-N-E ' 2p '/etc/sed.ini


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.