Linux Basic Management--sed (Text Processing musketeers)

Source: Internet
Author: User
Tags line editor


1. Introduction to SED


Sed, stream editor, line editor, because of its character syntax, is equivalent to a specialized language.


Sed is a stream editor that processes a single line of content at a time. When processing, the currently processed rows are stored in a temporary buffer called pattern space, followed by the SED command to process the contents of the buffer, and after processing is done, the contents of the buffer are sent to the screen. Then read the downstream line and execute the next loop. If you do not have a special command such as ' D ', it will empty the pattern space between the two loops, but will not empty the reserved space. This keeps repeating until the end of the file. The file content does not change unless you use redirection to store the output.


Function: Mainly used for automatic editing of one or more files, simplifying the repeated operation of files, writing conversion programs, etc.;

Reference:

Http://www.gnu.org/software/sed/manual/sed.html



2. How to use SED


2.1. Grammar


Grammar:

sed [option] ... ' Script ' Inputfile ...


Common options:

-N: does not output mode space content to the screen, that is, does not automatically print-e: Multipoint edit-f:/path/script_file: Read the edit script from the specified file-r: Support for using extended regular expressions-i.bak: Backing up files and editing them in situ


Script: ' Address command '.



2.2. Address Delimitation

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

(2) Single address:

#: Specified line, $: Last line/pattern/: Each row that the pattern can match to


(3) Address range:

#,# #,+#/pat1/,/pat2/#,/pat1/

(4) ~: Step forward

Odd rows 2~2 even rows


2.3. Edit command


D: Delete the pattern space matching row, and immediately enable the next round of the loop p: print the current mode space content, append to the default output a [\]text: Append text after the specified line, support using \ n to implement multiline append I [\]text: Insert text in front of line C [\] Text: Replace behavior single-line or multiline text W/path/somefile: Save pattern matching line to specified file R/path/somefile: reads the text from the specified file to the line that matches the pattern space =: Prints the line number for the line in the pattern space!: Matching row inversion in pattern space


2.4. Replacement tool


s///: Find replacement, support use of other separators, [email protected]@@,s###


Replace tag:

G: In-line global substitution P: Display replace successful row w/path/to/somefile: Save the replacement row to a file



2.5. Sed edit tool, replacement tool example


Sed ' 2p '/etc/passwdsed–n ' 2p '/etc/passwdsed–n ' 1,4p '/etc/passwdsed–n '/root/p '/etc/passwdsed–n ' 2,/root/p '/etc/pa SSWD starts from 2 lines sed-n '/^$/= ' file displays blank line number sed–n–e '/^$/p ' –e '/^$/= ' filesed '/root/a\superman '/etc/passwd ' after line sed '/root/i\supe Rman '/etc/passwd line before sed '/root/c\superman '/etc/passwd instead of line


Sed '/^$/d ' filesed ' 1,10d ' filenl/etc/passwd | Sed ' 2,5d ' nl/etc/passwd | Sed ' 2a tea ' sed ' s/test/mytest/g ' examplesed–n ' s/root/&superman/p '/etc/passwd word after sed–n ' s/root/superman&/p '/ etc/passwd words before Sed-e ' s/dog/cat/'-e ' s/hi/lo/' petssed–i.bak ' s/dog/cat/g ' pets



2.6. Advanced editing commands


P: Print mode space start to \ n content and append to default output h: Overwrite content in pattern space to hold space H: Append content in pattern space to hold space G: Remove data from hold space to pattern space G: Append from Hold space to pattern space X: Swap the contents of the pattern space with the content in the Hold space N: reads the next line of matching rows to the pattern space N: reads the next line of the matched rows to the pattern space D: Delete the row in the pattern space D: If the pattern space contains a newline character, delete until the first The line breaks the text in the pattern space and does not read the new input line, but uses the composition's pattern space to restart the loop. If the pattern space does not contain a newline character, the normal new loop is started as if the D command was emitted


Examples of sed:

Sed-n ' n;p ' FILE sed ' 1! G;h;$!d ' FILE sed ' n;d ' FILE sed ' $! n;$! D ' file sed ' $!d ' file sed ' g ' file sed ' g ' file sed '/^$/d; G ' file sed ' n;d ' file Sed-n ' 1! g;h; $p ' FILE


Linux Basic Management--sed (Text Processing musketeers)

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.