SED manual - 1. Introduction http://phi.sinica.edu.edu.tw (2001-07-29 08:05:00) 1.Intuction SED (Stream Editor) is on the UNIX system Provide editors that will automate editing work, users don't need to edit directly. Users can use SED to provide more than 20 different function parameters, combined (solution [1]) they complete different editing actions. In addition, since the SED is edited in behavioral units, it is also a line editor. General SEDs are most commonly used in editing those who need to continue to overcome some editing actions, such as replacing some strings in the file into another string, and the like. These modify files compared to the general Unix editor (talk, such as VI, Emacs) to modify files, and SED is more labor-saving. The following sections will introduce: When to use SED where SED SED can do those editing action SED how to work 1.1 When using SEDs When modifying files, if you continue to resize some editing actions, you can use SED automatic execution. These editing actions. For example, to make the sender name "Tom" in the ReceiveD file in the electronic letter to "Tom", "John", as long as the simple SED command is executed on the command column, you can put the "Tom" string in the file. Replace "John". Furthermore, when the file requires many different editing actions, SED can perform those different editing actions at a time. For example, the SED can perform all blank lines in the file, replace the string, and add the text of the user in the sixth line of the file, and the like. 1.2 Where is the SED average UNIX system, it is an SED. The Sed versions attached to the Different UNIX systems are not the same. If SED is not attached to the UNIX system used by the reader, you can get through anonymous ftp to the following places: phi.sinica.edu.tw:/pub/gnu/gnu getE.sinica.edu.tw:/unix/ GNU ftp.edu.tw:/unix/gnu ftp.csie.nctu.edu.tw:/pub/unix/gnu ftp.fcu.edu.tw: / pub3 / unix / gnu axp350.ncu.edu.tw:// Packages / gnu leica.ccu.edu.tw: / pub2 / gnu mail.ncku.edu.tw: / pub / unix / gnu bbs.ccit.edu.Tw: / pub1 / unix / gnu preip.ai.mit.edu .tw: / pub / gnu 1.3 SED can do those editing actions, change, add (Append), insert, in exchange, exchange files, or read other files Data into the file, also replace (Substuite) where the strings there are, or the transformation (TRANFER), etc. For example, the continuous blank line in the file is deleted into a line, and the "local" string is replaced into "Remote", "T" letters into "T", and the 10th line of data is combined with the 11th information. 1.4 How to work like other UNIX commands, the SED reads the editing file by the standard input and sent the result by the standard output. The following figure shows that the SED will replace the data line "UNIX", in the figure, the topardIrd INPUT is the standard input, which is the reading information; STANDARD OUTPUT is the standard output, which is the result of the results; the middle SED block The following two dashed squares represent the workflow of the SED. Among them, the left dotted block represents the SED to place the standard input data into the Pattern Space, and the right sides indicate that the SED is sent to the standard output of the data after editing in PATTERN Space.
In the dotted block, two solid blocks represent Pattern Space and Sed Script, respectively. Among them, Pattern Space is a cockpit area, which is a SED workplace; while the SED Script represents a set of executed editing instructions. In the figure, the left dotted block "UNIX" is placed in Pattern Space by the standard input; then, in the novel line block, SED performs editing instructions S / UNIX / UNIX / (solution [2]) in SED SCRIPT, and the result "Unix "Replace" UNIX ", then" UNIX "is sent by Pattern Space to the standard output. He is said that when the SED reads a row of information and puts the Pattern Space, the SED performs editing in accordance with the data of the editing instruction of the SED SCRIPT, and then it is sent from the results within the Pattern Space. Standard output, then read the next line of data. So far, the above action is reached until all data behavior is read. SED Manual - 2. Using Sed Http://phi.sinica.edu.tw aspac@phi.sinica.edu.tw (2001-07-29 09:05:00) SED command column can be divided into edit instructions and file files Part. Among them, the editing instruction is responsible for controlling all editing work; the file file represents the file being processed. The editing instructions of the SED are each consisting of the address and function (function), where the SED uses its location parameters to determine the object of editing; and use its function parameters (solution [3 ])edit. In addition, the SED editing instruction can be executed within the file in addition to the command column. The difference is only to be executed on the command column, and it must be added to the options -e; and in the file (Solution [4]), simply add option -f before its file name. In addition, the SED execution editing instruction is in accordance with them on the order or in the order. In the following sections, the editing instructions, SED editing instructions on the command column will be introduced, and the editing instructions in the execution file, perform editing of multiple files, and perform SED output control. 2.1 End Editing Instructions 2.2 Sed Editing Instruction 2.3 Sid Editing Instruction 2.3 Operating Instructions 2.4 Performing a Multiple Archmarks 2.5 Performing SED Output Control 2.1. To perform the editing instructions on the command column When editing instructions (see [section 2.2]) When executed on the command column, you must add options -E. The format of its command is as follows: Sed -E 'Editor Directive 1' -e 'Editing Instruction 2' ... file files, all editing instructions are followed by option -e, and placed in two "'" special characters between. In addition, the execution of the editing instruction on the command is left and right. Generally, when the editing instruction is not allowed, the user usually executes them directly on the command. For example, delete 1 to 10 lines of information within YEL.DAT, and change the "Yellow" string in its text to the "Black" string. At this point, the editing instruction can be executed directly on the command, and the command is as follows: Sed -e '1, 10d' -e 's / yellow / black / g' YEL.DAT in the command, editing instructions '1, 10d' (Solution [5]) Execute 1 to 10 lines of data; editing instructions 's / yellow / black / g' (Solution [6]), "Yellow" string replacement (Substuite) is "Black" string. 2.2 SED Editor The format of the SED editing instruction is as follows: [Address1 [, address2]] function [argument] where the address parameter address1, address2 is the number of rows or regular expression strings, indicating the edited data line; function parameters Function [argument] is the editing action of the execution of the SED.
The following two sections will carefully describe the representations of the location parameters and which function parameters are available. 2.2.1 Address parameter representation actually, the location parameter representation is only the data to be edited, and the number of rows or strings are used in place. The following is a few examples (instructions are used as a function parameter d (refer to [section4.2]): Delete the 10th line of the file, the instruction is 10d. When you delete the information line containing the "Man" string, the instruction is / man / d. Delete the 10th line to 200 row of information in the file, the instruction is 10,200d. Deleting the 10th line of the file to the data line with the "Man" string, the instruction is 10, / man / d. Next, the representation of the order parameters in the order parameters, and the representation of the order parameters in the command (also as an example of the function parameter D). The content of the location parameters: Address is a 10-in number: This number represents the number of rows. When the instruction is executed, the editing action of the function parameter indicated by the data that meets the number of this row is executed. For example, delete the 15th line of data in the data file, the instruction is 15d (refer to [section4.2]). It is pushed, such as the MD data in the data file, the command is MD. Address to Regular Expression (Refer to [Appendix A]): When there is a string representing REGULAR EXPRESSION, the editing action indicated by the function parameter is executed. In addition, "/" must be added before and after regone. For example, the instruction is /t.*T/d, indicating that all data lines containing two "T" letters. Among them, "." Indicates any font; "*" indicates that its preamble can be repeated, and they combine ". *" Represents any string between the two "T" letters. Number of adjacent parameters: In the instruction When there are two address parameters, such as address1, address2, indicate the editing of the data area, and address1 represents the starting information line, and the Address2 represents the Data Line. For the above, the following example is described. For example, the instruction is D which represents all the information lines within the file. For example, the instruction is 5D, which means the fifth line of the delete file. For example, the instruction is 1, / apple / d, which represents the delete data area, and has the data line of the "Apple" string in the first line in the file. For example, instructions are / Apple /, / Orange / D, which represents delete data zones, contains "Apple" strings in the file to data lines containing "Orange" strings 2.2.2 Some functions (function) Parameters lower page table This section describes the function parameters of all SEDs (refer to [Chapter 4]). Function parameters function: Label establishes the location of the Script File instructions. # Establish the solution {} set with instructions with the same address parameters. ! Do not perform function parameters. = Printing the number of data (LINE NUMBER). a Add the information entered by the user. b Label jumps to the executed instruction to the reference location established. c Substituting information entered by the user. D Delete information. D Delete the first newline letter before the Pattern Space. G copy information from Hold Space. G Add information from Hold Space to Pattern Space. H copy information from Pattern Space to Hold Space. H Add information from Pattern Space to Hold Space. l Printing the NonPrinting Character in the information with the ASCII code. i inserts the information line that adds the user input. N read into the next information. N Add the next information to Pattern Space. P Printing information. P Prints the information before the first newline letter in Pattern Space. Q jumps out of the SED editing. r read into its file content. s replace the string.
T Label performs a replacement editing instruction. If replaced into cow P>, you skip the editing instruction to: Label is executed. W Write information to its file. X Exchange Hold Space and Pattern Space content. Y Transform Volume. Although the SED is only a function of the basic editing functions, the SED is made by the mating between the order parameters and the instructions in the command, and the SED will complete most of the editing tasks. 2.3 Employment instructions in the Perform file When the execution is too much, it is very confusing on the command column. At this time, these instructions can be organized in the file (such as the file name Script_File), with option -f script_file, then Let SED perform editing instructions in the Script_File. The lack of their own order is as follows: SED -F script_file file files, where the order of execution of the editing instruction inside the Script_File is upward. For example, an example of the previous section can be changed to the following command: Sed -f ysb.scr Yel.dat where the YSB.scr file is as follows: 1, 10D S / YELLOW / BLACK / G In addition, on the command column Mixing Use Options - E and -F, the SED execution instruction sequence is still the left to right of the command column, as executed, if executed to the -f rear file, is executed. 2.4 Executing the editing of multiple file files On the SED command column, you can perform editing multiple file files at a time, they follow the editing instruction. To, .dat The above command executes, the SED is in the order of White.dat, Red.dat, Black.dat, executes editing instructions S / Yellow / Blue / (see [Section 4.1], performing a string replacement. 2.5. Export Controlled on the command column - N (Solution [7]) indicates the output by the editing instruction. By the previous chapter, the SED will "automatically" transfer the information from the Pattern Space to the standard output file. But by option - n, the SED This "automatic" action is changed to "passive" by the editing instruction it executes (Solution [8]) determines whether the result is output. Used by the above, the option -n must cooperate with the editing instruction Otherwise, the result is not available. For example, the WHITE.DAT file is printed with the "White" string, and the command is as follows: Sed -n -e '/ white / p' White.dat The above command, option -n To match the editing instruction / white / p (refer to [Section4.6]). Where, the option -n will output the output control power to the editing instruction; / White / P prints the "White" string in the data line Screen. SED Manual - 3. Example http://phi.sinica.edu.tw aspac@phi.sinica.edu.tw (2001-07-29 13:05:00) Generally in the actual use of the editor, It is often necessary to perform a string, move, delete, and search information, etc., etc. Of course, the general conversation editor (such as Vi, Emacs) can do the above function, but once the file has a large number of editing requirements When you use them to edit it. This chapter will use the example how to automatically perform these editing functions. In addition, in this chapter, all the following methods described above describe the requirements: in the file ... information, To perform ... (Action), the purpose is to quickly transfer them to edit instructions. Among them, the "... Data" part, the location parameter in the command is represented; "Execute ... Action" Some, then the transfer is a function parameter representation.
In addition, when "executing ... Action" is represented by several function parameters, "{" and "}" can be utilized (XL [9]), and the instruction form is as follows: Address parameter {function Parameter 1 Function Parameters 2 Function Parameters 3.:} The above instruction represents the operation of the order parameter, the function parameter 1, the function parameter 2, and the function parameter 3 ... represents the data. The following sections, separately illustrate the SED replacement information, mobile, delete information, and commands for searching information. 3.1 Replacement Document 3.2 Monitoring Document 3.3 Deleting Files 3.4 Profile in Search files 3.1 Replacement files SED replacement files in files in files, data lines, and even data areas. Among them, the function parameters in instructions indicating the replacement string are s (refer to [section4.1]); indicating that the function parameters in the instructions of the replacement information, or the data area are c (refer to [section4.5]). The above case will be described below. The above case will be described below. For example, the "PHI" string in the data line containing the "Machine" string is replaced with the "Beta" string. The order is listed as follows: Sed -e '/ Machine / S / PHI / BETA / G' INPUT.DAT (after the file file is represented by Input.dat). The 5th line of the file is replaced with sentence "Those Who in quarrels interpose, Must OFTEN WIPE A BLOODY NOSE. ". Its command is Sed -E '5C Those Must Offen Wipe A Bloody Nose.' INPUT.DAT Example III 3. Replace the data area of 1 to 100 lines in the file, replace it into the following two lines of information: How are you? Data be deleted! Then, its command is as follows Sed -E '1,100c how are you? Data be deleted!' Input.dat 3.2 Monitor users can use the information in the HOLD SPACE to temporarily store data in editing, with function parameters W ( Refer to [Section4.9]) Move file data to its file, or use function parameter R (refer to [Section4.8]) to move its files to the file. Hold Space is a tentry to temporarily save the data of the data in Pattern Space. When the SED performs function parameter h, h (refer to [Section4.19]), the Pattern Space data will be temporarily stored in Hold Space; when performing function parameters When X, G, G (Refer to [Section4.22]), the temporary data will be taken to Pattern Space. Three examples will be given below. Example 1. Plate the top 100 in the file to move to the 30000 data in the file. The order is as follows: SED -F MOV.scr file MOV.SCR file content is 1,100 {h d} 300g, 1,100 {h d} It represents the first 100 data in the file, first store (see [Section4. 19]) After the Hold Space is deleted; the instruction 300g (refer to [Section4.22]) indicates that the information within the Hold Space is added to the file 300 data. Example II. Documentation of the "PHI" string in the file is moved to the Mach.inf file. The order is listed as follows: Sed -e '/ pHI / W Mach.inf' file file Example 3. Wiping the Mach.inf file, moving to the document containing the "beta" string. Its command is as follows: Sed -e '/ beta / r mach.inf' file files, since the SED is a stream (refer to [section1.4]) editor, the same file data cannot be moved again. edit.
3.3 Deleting the information in the file Because the SED is an editor, the SED is easy to delete individual data lines or the entire data area. The function parameter d (refer to [Section4.2]) or D (refer to [Section4.17]) is used. Next, two examples will be described below. Remove all blank lines in the file. Its command is listed as SED -E '/ ^ $ / d' file file regular expression (Solution [Appendix A]), ^ $ indicates blank line. Among them, ^ limits the back strings must be in the order; $ Limit its preamble must be at the end. Remove them in a row of consecutive blank lines. Its command is listed as SED -E '/ ^ $ / {n / ^ $ / d}' file file, the function parameter n (refer to [Section4.16]), adds the next line of the blank line to Pattern Space . The function parameter / ^ $ / D indicates that when the added blank row is added, the first line of blank line is deleted, and the remaining blank line will re-execute the instruction. Once the command re-executed, delete a row of blank lines, so that the white line is added until the blank line is not blank, the last blank line is output. 3.4 Search files in the search file can perform the functionality similar to the UNIX command GREP. In theory, Regular Expression can be used (refer to [Appendix A]). For example, the file contains the data line output in the "Gamma" string. Then, the command is as follows: Sed -n -e '/ gamma / p' file file, but the SED is a row editor, and its search is basically in units of acts. Therefore, when some strings are disassembled into two parts due to a line, the general method is not feasible. At this point, you must search for these information in a two-line manner. The situation is as follows: Example. Output of the "Omega" string in the file. The contents of the SED -F GP.SCR file gear GP.SCR file are as follows: / omega / b n hs /.* // / omega / bg D in the above SED Script (solution [10]), due to borrow The function parameter B forms a Case Statement structure in a C language so that the SED can handle the "Omega" string in the data; when the "Omega" string is detached; and there is no "omega" string in the data Happening. Next, the SED Script is discussed in three parties in the above circumstances. When the data contains "Omega", then the editing instruction / OMEGA / B is performed. When the data is included, the SED does not need to perform the following instructions on it, and output it directly. When there is no "Omega" in the data, the editing instruction is executed as follows /.* // / omega / b where the function parameter n (refer to [Section 4.16]), which means reading the next line of data to make the pattern space Two lines of data before and after. Function parameter h (Refer to [Section 4.19]), it indicates that the two-line data within the Pattern Space is stored in Hold Space. Function parameters S ////, it represents a row of two-line data in the Pattern Space ([11]). / OMEGA / B, it indicates that if the following information is included in the "Omega" string, you don't have to execute it after it is executed, and this data is automatically output; when the following information still does not include "Omega", then execute The editing instructions are as follows G D, and the function parameter g (refer to [Section4.21]), which represents the two lines of the Hold Space in the Pattern Space. Function parameter d (Refer to [Section4.17]), it deletes the first line of data in both lines of data, and lets the remaining information, re-execute the Sed Script. Thus, the string of the data or the string in the line can be used to search.
SED Manual - 4. Introduction Function Parameters http://phi.sinica.edu.tw aspac@phi.sinica.edu.tw (2001-07-30 07:00:00) This chapter will take a function parameter in a section Introduce all SED provided by the function parameters, including | s | d | a | i | c | p | l | r | w | y |! | N | q | = | # | n | d | p | h | H | g | g | x | situation. 4.1 S function parameter s represents a substitution file internal string. Its instruction format is as follows: [address1 [, address2]] s / pattern / replacemen / [flag] There is a lower description of the above format: Function parameter S is up to two address parameters. About "S / Pattern / Replacement / [FLAG]" (Solution [12]) has the following description: pattern: It is a REGULER Expression string. It represents the string to be replaced in the file. Replacement: It is a general string. However, there is a special meaning of the following character: &: representing the Pattern string in front. For example, the Sed -E 'S / Test / & my car /' data file name instruction, & represents "TEST" in the Pattern string. Therefore, after execution, the "TEST" of the data file is replaced with "Test My Car". : The string enclosed by the nth (,) (refer to [Appendix A]) in Pattern. For example, Sed -E 'S / (TEST) (MY) / [2 3 1] /' Data Name Instruction, 1 means "Test", 2 indicates "MY", 1 means "Car" string. Therefore, after execution, the "Test My Car" of the data file is replaced into "[My Car Test]." : Can be used to restore some of the meaning of special symbols (such as the & and) itself, or use it to represent the wrap. Flag: Mainly used to control some replacement: When the FLAG is G, the representative replaces all the strings that match (MATCH). When the FLAG is a ten-in number M, represents the square string in line in the row. When Flag is P, after replacing the first string that conforms to Pattern, the data outputs the standard output file. When Flag is W WFILE, the representative replaces the first string that conforms to Pattern, and outputs it within the WFILE file (if WFILE does not exist, the file named WFile will be reopened). When there is no FLAG, the first string of the data in line with Pattern is replaced by the Replacement string. Delimiter: "/" in "/" / "" is used as a Delimiter. In addition to the blank, the NEWLINE, the user can use any character as DELIMITER. For example, the following editing instructions S # / usr # / usr1 # g in the above command VERB | # | is Delimiter. If you use "/" to do Delimiter, the SED will errors in Pattern and Replacement as Delimiter. Example: Topic: Replace Input.dat file (there is no special specification, assume that the file file is "1996" string into "1997", and the data line is stored in Year97.dat.
Note: The function parameter S indicates that the SED will replace the "1996" string into "1997", and the FLAG W in S Argument indicates that the SED will replace the data line in Year97.dat. SED command column: SED -E 'S / 1996/1997 / W Year97.dat' Input.dat 4.2 D Function Parameters D Represents the delete data line, its instruction format is as follows: [Address1 [, address2]] D. above the above format Some explanations: The function parameter d is up to two address parameters. The SED performing the delete action is as follows: Delete the data that meets the order parameters in Pattern Space. Read the next information into the Pattern Space. Re-execute the SED Script. Example: You can refer to Section 3.3. 4.3 A Function Parameters a Represents the data to the file. The format of its instructions are as follows: [address1] A user-entered data The above format has the following description: The function parameter A is up to one address parameter. The function parameter A follows "" "" "The character is used to indicate that the information entered by the user must be entered from the next row. If the data exceeds a line, it must be added at the end of each row. The SED performing the addition action is as follows: When the data output in the Pattern Space, the SED follows the information entered by the output user. Example: Topic: Add the "Multi-work System" after the line with the "UNIX" string. Suppose the contents of the input.dat file are as follows: UNIX Description: Use the function parameter a to add the input data to the data line containing the "UNIX" string. The SED command is listed as follows: Sed -e '/ UNIX / A Multi-work System' Input.dat After performing the above command, its output is as follows: UNIX Multi-work System 4.4 I Function Parameters i represents the data inserted into the file. Its instruction format is as follows: [address1] IN the user's format has the following description: The function parameter i is up to one address parameter. The function parameter i is followed by "" The character is used to indicate the end, and the information entered by the user must be entered from the next row. If the data exceeds a line, it must be added at the end of each row. The case where the SED performs insertion action is as follows: Before the data output in the Pattern Space, the SED will first output the information entered by the user. Example: Topic: Plug the "Central Research Institute" in the Input.dat file in Input.dat, "Dean: Li Yuanzhe". Suppose the Input.dat files are as follows: Dean: Li Yuanzhe Description: Use the function parameter i to line the article "Article copyright belongs to the Central Research Institute" inserted before the information of "Dean: Li Yuanzhe". The SED command is listed as follows: Sed -e '/ Dean: Li Yuanzhe / i article copyright belongs to the Central Research Institute' Input.dat Execute the output after the above command is as follows: The copyright of the article belongs to the Dean of the Central Research: Li Yuanzhe 4.5 c function parameter c Change the information in the file. The format is as follows: [address1 [, address2]] C users have the following descriptions of the above format: The function parameter C is up to two address parameters. The function parameter c is followed by "" "The character is used to indicate that the information entered by the user must be entered from the next row. If the data exceeds a line, it must be added at the end of each row. SED performs changing the action: When the data output in the Pattern Space, the SED changes it into the information entered by the user. Example: Refer to the second, three of the sections 3.1. 4.6 P Function Parameters P Represents the printed information. The format of its instructions are as follows: [address1 [, address2]] p For the above format: The function parameter P is up to two address parameters.