Hệ Điều Hành Linux (P13) potx

30 258 0
Hệ Điều Hành Linux (P13) potx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

< Day Day Up > Page 361 ABC Amber CHM Converter Trial version, http://www.processtext.com/abcchm.html Editor Basics A sed program consists of one or more lines with the following syntax: [address[ ,address] ] instruction [argument-list] The addresses are optional. If you omit the address, sed processes all lines from the input. The instruction is the editing instruction that modifies the text. The addresses select the line(s) that the instruction part of the command operates on. The number and kinds of arguments in the argument-list depend on the instruction. If you want to put several sed commands on one line you can separate the commands with semicolons (;). The sed utility processes input as follows: 1. 1. Reads one line of input from file-list or standard input. 2. 2. Reads the first instruction from the program or program-file. If the address(es) select the input line, acts on the input line as the instruction specifies. 3. 3. Reads the next instruction from the program or program-file. If the address(es) select the input line, acts on the input line (possibly modified by the previous instruction) as the instruction specifies. 4. 4. Repeats step 3 until it has executed all instructions in the program or program-file. 5. 5. Starts over again with step 1 if there is another line of input; otherwise, it is finished. Addresses A line number is an address that selects a line. As a special case, the line number $ represents the last line of input. A regular expression (refer to Appendix A) is an address that selects those lines containing a string that the regular expression matches. Although slashes are often used to delimit these regular expressions, sed permits you to use any character other than a backslash or NEWLINE for this purpose. Except as noted, zero, one, or two addresses (either line numbers or regular expressions) can precede an instruction. If you do not use an address, sed selects all lines, causing the instruction to act on every line of input. Providing one address causes the instruction to act on each input line that the address selects. Providing two addresses causes the instruction to act on groups of lines. The first address selects the first line in the first group. The second address selects the next subsequent line that it matches; this line is the last line in the first group. If no match for the second address is found, the second address points to the end of the file. After selecting the last line in a group, sed starts the selection process over again, looking for the next line that the first address matches. This line is the first line in the next group. The sed utility continues this process until it has finished going through the entire file. Instructions d (delete) The Delete instruction causes sed not to write out the lines it selects and not to finish processing the lines. After sed executes a Delete instruction, it reads the next input line and begins over again with the first instruction from the program or program-file. n (next) The Next instruction writes out the currently selected line if appropriate, reads the next input line, and starts processing the new line with the next instruction from the program or program-file. a (append) The Append instruction appends one or more lines to the currently selected line. If you precede an Append instruction with two addresses, it appends to each line that is selected by the addresses; earlier versions of sed did not accept Append instructions with two addresses. If you do not precede an Append instruction with an address, it appends to each input line. An Append instruction has the following format: [address[,address]] a\ text\ text\ text You must end each line of appended text, except the last, with a backslash (the backslash quotes the following NEWLINE). The appended text concludes with a line that does not end with a backslash. The sed utility always writes out appended text, regardless of whether you use a –n flag on the command line. It even writes out the text if you delete the line to which you are appending the text. i (insert) The Insert instruction is identical to the Append instruction except that it places the new text before the selected line. c (change) The Change instruction is similar to Append and Insert except that it changes the selected lines so that they contain the new text. When you specify an address range, Change replaces the entire range of lines with a single occurrence of the new text. s (substitute) The Substitute instruction in sed is similar to that in vim (page 166). It has the following format: [address[,address]] s/pattern/replacement-string /[g][p][w file] The pattern is a regular expression (refer to Appendix A) that is delimited by any character other than a SPACE or NEWLINE, traditionally a slash ( / ). The replacement-string starts immediately following the second delimiter and must be terminated by the same delimiter. The final (third) delimiter is required. The replacement-string can contain an ampersand (&), which sed replaces with the matched pattern. Unless you use the g flag, the Substitute instruction replaces only the first occurrence of the pattern on each selected line. The g (global) flag causes the Substitute instruction to replace all nonoverlapping occurrences of the pattern on the selected lines. The p (print) flag causes sed to send all lines on which it makes substitutions to standard output. This flag overrides the –n option on the command line. The w (write) flag is similar to the p flag but it sends its output to the file specified by file. A single SPACE and the name of the output file must follow a w flag. p (print) The Print instruction writes the selected lines to standard output, writing the lines immediately, and does not reflect the effects of subsequent instructions. This instruction overrides the –n option on the command line. (The –n option prevents sed from copying lines to standard output.) w file (write) This instruction is similar to the Print instruction except that it sends output to the file specified by file. A single SPACE and the name of the output file must follow a Write instruction. r file (read) The Read instruction reads the contents of the specified file and appends it to the selected line. A single SPACE and the name of the input file must follow a Read instruction. q (quit) The Quit instruction causes sed to terminate immediately. Control Structures ! (NOT) Causes sed to apply the following instruction, located on the same line, to each of the lines not selected by the address portion of the instruction. For example, 3!d deletes all lines except line 3 and $!p displays all lines except the last. { } (group instructions) When you enclose a group of instructions within a pair of braces, a single address (or address pair) selects the lines on which the group of instructions operates. Use semicolons (;) to separate multiple commands appearing on a single line. Branch instructions The sed info page lists the branch instructions as "Commands for sed gurus" and suggests that if you need them you might be better off writing your program in awk or Perl. : label Identifies a location within a sed program. The label is useful as a target for the b and t branch instructions. b [label] Unconditionally transfers control (branches) to label. Without label, skips the rest of the instructions for the current line of input and reads the next line of input (page 565). t [label] Transfers control (branches) to label only if a Substitute instruction has been successful since the most recent line of input was read (conditional branch). Without label, skips the rest of the instructions for the current line of input and reads the next line of input (page 565). The Pattern Space and the Hold Space The sed utility has two buffers. The commands reviewed up to this point work with the Pattern space, which initially holds the line of input that sed just read. The Hold space can hold data while you manipulate data in the Pattern space; it is a temporary buffer. Until you place data in the Hold space it is empty. This section discusses commands that move data between the Pattern space and the Hold space. g Copies the contents of the Hold space to the Pattern space. The original contents of the Pattern space is lost. G Appends a NEWLINE and the contents of the Hold space to the Pattern space. h Copies the contents of the Pattern space to the Hold space. The original contents of the Hold space is lost. H Appends a NEWLINE and the contents of the Pattern space to the Hold space. x Exchanges the contents of the Pattern space and the Hold space. Page 362 ABC Amber CHM Converter Trial version, http://www.processtext.com/abcchm.html < Day Day Up > Page 363 ABC Amber CHM Converter Trial version, http://www.processtext.com/abcchm.html < Day Day Up > Page 364 ABC Amber CHM Converter Trial version, http://www.processtext.com/abcchm.html Examples new data file The following examples use the input file new: $ cat new Line one. The second line. The third. This is line four. Five. This is the sixth sentence. This is line seven. Eighth and last. Unless you instruct it not to, sed sends all lines—selected or not—to standard output. When you use the –n option on the command line, sed sends only certain lines, such as those selected by a Print (p) instruction, to standard output. The following command line displays all the lines in the new file that contain the word line (all lowercase). In addition, because there is no –n option, sed displays all the lines of input. As a result the lines that contain the word line are displayed twice. $ sed '/line/ p' new Line one. The second line. The second line. The third. This is line four. This is line four. Five. This is the sixth sentence. This is line seven. This is line seven. Eighth and last. The command uses the address /line/, a regular expression that is a simple string. The sed utility selects each of the lines that contains a match for that pattern. The Print (p) instruction displays each of the selected lines. The following command uses the –n option so that sed displays only the selected lines: $ sed -n '/line/ p' new The second line. This is line four. This is line seven. Next sed displays part of a file based on line numbers. The Print instruction selects and displays lines 3 through 6. $ sed -n '3,6 p' new The third. This is line four. Five. This is the sixth sentence. The next command line uses the Quit instruction to cause sed to display only the beginning of a file. In this case sed displays the first five lines of new just as a head –5 new command would. $ sed '5 q' new Line one. The second line. The third. This is line four. Five. program-file When you need to give sed more complex or lengthy instructions, you can use a program-file. The print3_6 program performs the same function as the command line in a previous example. The –f option tells sed that it should read its program from the file named on the command line. $ cat print3_6 3,6 p $ sed -n -f print3_6 new The third. This is line four. Five. This is the sixth sentence. Append The next program selects line 2 and uses an Append instruction to append a NEWLINE and the text AFTER. to the selected line. Because the command line does not include the –n option, sed copies all the lines from the input file new. $ cat append_demo 2 a\ AFTER. $ sed -f append_demo new Line one. The second line. AFTER. The third. This is line four. Five. This is the sixth sentence. This is line seven. Eighth and last. Insert The insert_demo program selects all the lines containing the string This and inserts a NEWLINE and the text BEFORE. before the selected lines. $ cat insert_demo /This/ i\ BEFORE. $ sed -f insert_demo new Line one. The second line. The third. BEFORE. This is line four. Five. BEFORE. This is the sixth sentence. BEFORE. This is line seven. Eighth and last. Change The next example demonstrates a Change instruction with an address range. When you specify a range of lines for a Change instruction, it does not change each line within the range but rather changes the block of lines to a single occurrence of the new text. $ cat change_demo 2,4 c\ SED WILL INSERT THESE\ THREE LINES IN PLACE\ OF THE SELECTED LINES. $ sed -f change_demo new Line one. SED WILL INSERT THESE THREE LINES IN PLACE OF THE SELECTED LINES. Five. This is the sixth sentence. This is line seven. Eighth and last. Substitute The next example demonstrates a Substitute instruction. The sed utility selects all lines because the instruction has no address. On each line subs_demo replaces the first occurrence of line with sentence. The p flag displays each line where a substitution occurs. The command line calls sed with the –n option, so sed displays only the lines that the program explicitly requests it to display. $ cat subs_demo s/line/sentence/p $ sed -n -f subs_demo new The second sentence. This is sentence four. This is sentence seven. The next example is similar to the preceding one except that a w flag and filename (temp) at the end of the Substitute instruction cause sed to create the file named temp. The command line does not include the –n option, so it displays all lines in addition to writing the changed lines to temp. The cat utility displays the contents of the file temp. The word Line (starting with an uppercase L) is not changed. $ cat write_demo1 s/line/sentence/w temp $ sed -f write_demo1 new Line one. The second sentence. The third. This is sentence four. Five. This is the sixth sentence. This is sentence seven. Eighth and last. $ cat temp The second sentence. This is sentence four. This is sentence seven. The following bash script changes all occurrences of REPORT to report, FILE to file, and PROCESS to process in a group of files. Because it is a shell script and not a sed program file, you must have read and execute permission to the sub file to execute it as a command (page 263). The for structure (page 451) loops through the list of files supplied on the command line. As it processes each file, the script displays each filename before processing the file with sed. This program uses multiline embedded sed commands. Because the NEWLINEs between the commands are quoted (placed between single quotation marks), sed accepts multiple commands on a single, extended command line (within a shell script). Each Substitute instruction includes a g (global) flag to take care of the case where a string occurs more than one time on a line. $ cat sub for file do echo $file mv $file $$.subhld sed 's/REPORT/report/g s/FILE/file/g s/PROCESS/process/g' $$.subhld > $file done rm $$.subhld $ sub file1 file2 file3 file1 file2 file3 In the next example, a Write instruction copies part of a file to another file (temp2). The line numbers 2 and 4, separated by a comma, select the range of lines sed is to copy. This program does not alter the lines. $ cat write_demo2 2,4 w temp2 $ sed -n -f write_demo2 new $ cat temp2 The second line. The third. This is line four. The program write_demo3 is very similar to write_demo2 but precedes the Write instruction with the NOT operator (!), causing sed to write to the file those lines not selected by the address. $ cat write_demo3 2,4 !w temp3 $ sed -n -f write_demo3 new $ cat temp3 Line one. Five. This is the sixth sentence. This is line seven. Eighth and last. The following example demonstrates the Next instruction. When it processes the selected line (line 3), sed immediately starts processing the next line without displaying line 3. $ cat next_demo1 3 n p $ sed -n -f next_demo1 new Line one. The second line. This is line four. Five. This is the sixth sentence. This is line seven. Eighth and last. The next example uses a textual address. The sixth line contains the string the, so the Next instruction causes sed not to display it. $ cat next_demo2 /the/ n p $ sed -n -f next_demo2 new Line one. The second line. The third. This is line four. Five. This is line seven. Eighth and last. The next set of examples uses the file compound.in to demonstrate how sed instructions work together. $ cat compound.in 1. The words on this page 2. The words on this page 3. The words on this page 4. The words on this page The following example substitutes the string words with text on lines 1, 2, and 3 and the string text with TEXT on lines 2, 3, and 4. The example also selects and deletes line 3. The result is text on line 1, TEXT on line 2, no line 3, and words on line 4. The sed utility made two substitutions on lines 2 and 3: text for words and TEXT for text. Then sed deleted line 3. $ cat compound 1,3 s/words/text/ 2,4 s/text/TEXT/ 3 d $ sed -f compound compound.in 1. The text on this page 2. The TEXT on this page 4. The words on this page The ordering of instructions within a sed program is critical. Both Substitute instructions are applied to the second line in the following example, as in the previous example, but the order in which the substitutions occur changes the result. $ cat compound2 2,4 s/text/TEXT/ 1,3 s/words/text/ 3 d $ sed -f compound2 compound.in 1. The text on this page 2. The text on this page 4. The words on this page Next compound3 appends two lines to line 2. The sed utility displays all the lines from the file once because no –n option appears on the command line. The Print instruction at the end of the program file displays line 3 an additional time. $ cat compound3 2 a\ This is line 2a.\ This is line 2b. 3 p $ sed -f compound3 compound.in 1. The words on this page 2. The words on this page This is line 2a. This is line 2b. 3. The words on this page 3. The words on this page 4. The words on this page The next example shows that sed always displays appended text. Here line 2 is deleted but the Append instruction still displays the two lines that were appended to it. Appended lines are displayed even if you use the –n option on the command line. $ cat compound4 2 a\ This is line 2a.\ This is line 2b. 2 d $ sed -f compound4 compound.in 1. The words on this page This is line 2a. This is line 2b. 3. The words on this page 4. The words on this page The next example uses regular expressions in the addresses. The regular expression in the following instruction (^.) matches one character at the beginning of every line that is not empty. The replacement string (between the second and third slashes) contains a backslash escape sequence that represents a TAB character (\t) followed by an ampersand (&). The ampersand takes on the value of whatever the regular expression matched. $ sed 's/^./\t&/' new Line one. The second line. The third. This type of substitution is useful for indenting a file to create a left margin. See Appendix A for more information on regular expressions. You can also use the simpler form s/^/\t/ to add TABs to the beginnings of lines. However, in addition to placing TABs at the beginning of lines with text on them, this instruction places a TAB at the beginning of every empty line—something the preceding command does not do. You may want to put the preceding sed instruction into a shell script so that you do not have to remember it (and retype it) each time you want to indent a file. The chmod utility gives you read and execute permission to the ind file. $ cat ind sed 's/^./\t&/' $* $ chmod u+rx ind $ ind new Line one. The second line. The third. Stand-alone script When you run the preceding shell script, it creates two processes: It calls a shell, which in turn calls sed. You can eliminate the overhead associated with the shell process by putting the line #!/bin/sed –f (page 265) at the start of the script, which runs the sed utility directly. You need read and execute permission to the file holding the script. $ cat ind2 #!/bin/sed -f s/^./\t&/ In the following sed program, the regular expression (two SPACEs followed by *$) matches one or more SPACEs at the end of a line. This program removes trailing SPACEs at the ends of lines, which is useful for cleaning up files that you created using vim. $ cat cleanup sed 's/ *$//' $* The cleanup2 script runs the same sed command as cleanup but stands alone: It calls sed directly with no intermediate shell. $ cat cleanup2 #!/bin/sed -f s/ *$// Hold space The next sed program makes use of the Hold space to exchange pairs of lines in a file. $ cat s1 h # Copy Pattern space (line just read) to Hold space. n # Read the next line of input into Pattern space. p # Output Pattern space. g # Copy Hold space to Pattern space. p # Output Pattern space (which now holds the previous line). $ sed -nf s1 new The second line. Line one. This is line four. The third. This is the sixth sentence. Five. Eighth and last. This is line seven. The commands in the s1 program process pairs of input lines. This program reads a line and stores it; reads another line and displays it; and then retrieves the stored line and displays it. After processing a pair of lines the program starts over with the next pair of lines. The next sed program adds a blank line after each line in the input file (i.e., it double-spaces a file). $ sed 'G' new Line one. The second line. The third. This is line four. $ The G instruction appends a NEWLINE and the contents of the Hold space to the Pattern space. Unless you put something in the Hold space, it is empty. Thus the G instruction copies a NEWLINE to each line of input before sed displays the line(s) from the Pattern space. The s2 sed program reverses the order of the lines in a file just as the tac utility does. $ cat s2 2,$G # On all but the first line, append a NEWLINE and the # contents of the Hold space to the Pattern space. h # Copy the Pattern space to the Hold space. $!d # Delete all except the last line. $ sed -f s2 new Eighth and last. This is line seven. This is the sixth sentence. Five. This is line four. The third. The second line. Line one. This program includes three commands: 2,$G, h, and $!d. To understand this script it is important to understand how the address of the last command works: The $ is the address of the last line of input and the ! negates the address. The result is an address that selects all except the last line of input. In the same fashion you could replace the first command with 1!G: Select all except the first line for processing; the results would be the same. Here is what happens as s2 processes the new file: 1. The sed utility reads the first line of input (Line one.) into the Pattern space. a. a. The 2,$G does not process the first line of input—because of its address the G instruction starts processing at the second line. b. b. The h copies Line one. from the Pattern space to the Hold space. c. c. The $!d deletes the contents of the Pattern space. Because there is nothing in the Pattern space, sed does not display anything. 2. The sed utility reads the second line of input (The second line.) into the Pattern space. a. a. The 2,$G adds what is in the Hold space (Line one.) to the Pattern space. The Pattern space now has The second line.NEWLINELine one. b. b. The h copies what is in the Pattern space to the Hold space. c. c. The $!d deletes the second line of input. Because it is deleted, sed does not display it. 3. The sed utility reads the third line of input (The third.) into the Pattern space. a. a. The 2,$G adds what is in the Hold space (The second line.NEWLINELine one.) to the Pattern space. The Pattern space now has The third.NEWLINE The second line.NEWLINELine one. b. b. The h copies what is in the Pattern space to the Hold space. c. c. The $!d deletes the contents of the Pattern space. Because there is nothing in the Pattern space, sed does not display anything. . . . 8. The sed utility reads the eighth (last) line of input into the Pattern space. a. a. The 2,$G adds what is in the Hold space to the Pattern space. The Pattern space now has all the lines from new in reverse order. b. b. The h copies what is in the Pattern space to the Hold space. This step is not necessary for the last line of input but does not alter the program's output. c. c. The $!d does not process the last line of input. Because of its address the d instruction does not delete the last line. d. d. The sed utility displays the contents of the Pattern space. Page 365 ABC Amber CHM Converter Trial version, http://www.processtext.com/abcchm.html < Day Day Up > Page 366 ABC Amber CHM Converter Trial version, http://www.processtext.com/abcchm.html < Day Day Up > Chapter Summary The sed (stream editor) utility is a batch (noninteractive) editor. It takes its input from files you specify on the command line or from standard input. Unless you redirect the output from sed, it goes to standard output. A sed program consists of one or more lines with the following syntax: [address[ ,address] ] instruction [argument-list] The addresses are optional. If you omit the address, sed processes all lines of input. The instruction is the editing instruction that modifies the text. The addresses select the line(s) the instruction part of the command operates on. The number and kinds of arguments in the argument-list depend on the instruction. In addition to basic instructions, sed includes some powerful advanced instructions. One set of these instructions allows sed programs to store data temporarily in a buffer called the Hold space. Other instructions provide unconditional and conditional branching in sed programs. < Day Day Up > Page 367 ABC Amber CHM Converter Trial version, http://www.processtext.com/abcchm.html < Day Day Up > Exercises 1: Write a sed command that copies a file to standard output, removing all lines that begin with the word Today. 2: Write a sed command that copies only those lines of a file that begin with the word Today to standard output. 3: Write a sed command that copies a file to standard output, removing all blank lines (lines with no characters on them). 4: Write a sed program named ins that copies a file to standard output, changing all occurrences of cat to dog and preceding each modified line with a line that says following line is modified. 5: Write a sed program named div that copies a file to standard output, copies the first five lines to a file named first, and copies the rest of the file to a file named last. 6: Write a sed command that copies a file to standard output, replacing a single SPACE as the first character on a line with a 0 (zero) only if the SPACE is immediately followed by a number (0–9). For example: abc abc abc abc 85c 085c 55b 55b 000 0000 7: How can you use sed to triple-space (i.e., add two blank lines after each line in) a file? < Day Day Up > Page 368 ABC Amber CHM Converter Trial version, http://www.processtext.com/abcchm.html Page 369 ABC Amber CHM Converter Trial version, http://www.processtext.com/abcchm.html < Day Day Up > Part V: Command Reference  Command Reference < Day Day Up > Page 370 ABC Amber CHM Converter Trial version, http://www.processtext.com/abcchm.html

Ngày đăng: 07/07/2014, 09:20

Tài liệu cùng người dùng

Tài liệu liên quan