Sed is a very powerful and small text stream editor. In the second article of this article, Daniel Robbins demonstrates how to use SED to perform string replacement, create a larger SED script, and how to use SED, insert, and change the line command.
The SED is a very useful (but often forgotten) UNIX stream editor. It is a very ideal tool to edit the file in a batch method or create a shell script in a valid manner. This article is the previous introduction of the seventh article. replace! Let's take a look at one of the most useful commands of the SED, replace the command. Use this command to replace a specific string or matching rule expression with another string. Below is an example of the most basic usage of the command: $ SED -E 'S / FOO / BAR /' MyFile.txt The command above myFile.txt's first "foo '(if any) String 'Bar' Replace, then output the file content to the standard output. Please note that I am talking about the first time, although this is usually not what you want. When performing a string, you usually want to perform global replacement. That is, to replace all the appearances in each row, as shown below: $ SED -E 'S / FOO / BAR / G' MyFile.txt Additional 'g' after the last slash tells SED to perform global replacement . Regarding the 's ///' replacement command, there are other things to know. First, it is a command and is just a command, and there is no address in all the above example. This means that 's ///' can also use the address to control which lines to apply the commands, as shown: $ sed -e '1, 10s / enchantment / entrapment / g' myfile2.txt The phrase 'entrapment' will be replaced with all the phrase 'Enchantment', but only doing this on the first to 10th lines (including these two lines). $ SED -E '/ ^ $ /, / ^ end / s / hills / mountains / g' myfile3.txt This example will replace 'hills' with 'mountains', but only starting from the idlts, to three characters The line ends of 'end' starts (including these two lines). Another thing about 's ///' command is the '/' separator has many replacement options. If a string replacement is being executed, and there are many slashes in the rule expression or replacement string, you can change the separator by specifying a different character after 'S'. For example, the following example will replace all the appearance / usr / local to / usr: $ sed -e 's: / usl / local: / usr: g' mylist.txt In this example, use the colon as a separator. If you need to specify a separator character in the rule expression, you can add a backslash in front of it. Rule Expressions So far, we only perform simple string replacement. Although this is very convenient, we can also match rule expressions. For example, the following sed commands will match the beginning of '<' start, to '>', and include any quantity characteristic phrase. The following example will delete the phrase (replace with an empty string): $ sed -e 's /<. However, due to the unique rules of the rules expressions, it will not work well. What is the reason? When the SED is trying to match the rule expression in the row, it is looking for the longest match in the row.
In my previous SED article, this is not a problem, because we use 'd' and 'p' commands, these commands should always be deleted or printed. However, when using the 's ///' command, it is indeed very different because the entire section of the rule expression will be replaced by the target string, or in this example, deleted. This means that the above example will drop down: this b> is what i b> meant. We want to do this, but: this is what i meant. Fortunately, there is a simple way to correct the problem. We do not enter "'<'" and follow the rule expressions of "'>' characters end", just enter a "'<' character back and any number of" Non-'"characters and" " The rule expression of 'character ending. This will match the shortest, not the longest possibility. The new command is as follows: $ SED -E 'S / <[^>] *> // g' MyFile.html In the previous example, '[^>]' specifies "non-'>" characters, after the' * 'Complete this expression to indicate "zero or more non-'> 'characters". Test the command for several HTML files, output their pipes to "more" and then look carefully. More Character Match '[]' Rule Expression Syntax has some additional options. To specify the character range, you can use '-' as long as the character is not in the first or last position, as shown below: '[ax] *' This will match zero or more all of 'A', 'b', 'c' ... 'V', 'w', 'x' characters. Alternatively, you can use the '[: Space:]' character class to match the space. The following is a fairly complete list of available characters: Character class description [: alnum:] alphanumeric [AZ AZ 0-9] [: alpha:] Letter [AZ AZ] [: blank:] space or tab. [: CNTR:] Any control character [: Digit:] [0-9] [: graph:] Any Visual Character (no space) [: Lower:] lowercase [AZ] [: print:] Non-control characters [: punct :] Patch characters [: space:] space [: Upper:] uppercase [AZ] [: xdigit:] Hexadecimal number [0-9 AF] is very advantageous as possible using character classes, because they can be more Good intended to adapt to English Locale (including certain essential stress characters, etc.). Advanced replacement features We have seen how to perform simple or even complex direct replacement, but SED can also do more things. In fact, some or all of the matching rule expressions can be referenced, and these parts can be used to construct replacement strings. As an example, suppose you are replying to a message. The following example will be added in front of each line "Ralph Said:": $ SED -E 'S /.*/ Ralph Said: & /' Origmsg.txt Output is as follows: Ralph Said: Hiya Jim, Ralph Said: Ralph Said:
I SURE LIKE THIS SEDSUFF! RALPH SAID: The '&' characters are used in the replacement string of this example, which tells the SED into the entire matching rule expression. Therefore, any content (the maximum group or whole line of zero or multiple characters in the row in the row) can be inserted into any location in the replacement string, even multiplexes. This is very good, but SED is even more powerful. Those extremely good parentheses 's ///' commands even better than '&', allow us to define the area in the rule expression, and then reference these specific areas in the replacement string. As an example, it is assumed that there is a file containing the following text: Foo Bar Oni Eny Yeeny Larry Curly Moe Jimmy THE Weasel now assumes that you want to write a SED script, this script will replace "Eny MINY" "Victor Eny-Meeny Von" " and many more. To do this, you must first write a rule expression that is separated by spaces and matches three strings. '. *. *. *' Now, insert a cracker with a reverse slash in each of the regions of interest to define the zone: '/(.*/) /(.*/) /(.*/ ) 'In addition to defining three logical areas that can be referenced in the standby string, the operating principle of this rule will be the same as the first rule expression. Here is the final script: $ sed -e 's //( (/) /(./victor / 1- / 2 von / 3 /' myfile.txt, if you see, By inputting '/ x' (where x is the area that is bound by the parentheses from 1). Enter the following: Victor Foo-Bar Von ONI VICTOR EENY-MEENY VON MINY VICTOR LARRY-CURLY VON MIN MINY VICTOR LARRY-CURLY VON MOE VICTOR JIMMY-CURLY VON MOE VICTOR JIMMY-THE VON Weasel With the more familiarity of SED, you can spend the minimum force to perform considerable text processing. You may want to use the familiar scripting language to handle this problem - Can you easily implement such a solution with a line of code? The combination uses the ability to enter multiple commands when you start creating more complex SED scripts. There are several ways to do so. First, a semicolon can be used between commands. For example, the following command series uses '=' command and 'p' command, '=' command tells the SED print line number, 'P' command clearly tells SED to print the row (because in '-N' mode). $ SED -N -E '=; p' MyFile.txt Whenever two or more commands are specified, each command is applied to each line of the file in order. In the above example, first apply the '=' command to the first line, then apply the 'p' command. Next, the SED continues to process the second line and repeat the process. Although the semicolon is very convenient, it does not work properly in some cases. Another replacement method is to use two-E options to specify two different commands: $ sed -n -e '=' -e 'p' myfile.txt However, when using more complex additional and inserted commands And even multiple '-e' options can not help us.
For complex multi-line scripts, the best way is to put the command in a separate file. Then, use the -f option to reference this script file: $ SED -N -F myCommands.sed myfile.txt This method may not be too convenient, but always tubercular. Multiple commands of an address are sometimes necessary to specify multiple commands applied to an address. This is especially convenient when performing a number of 's ///' in the change in the source file. To perform multiple commands to an address, you can enter the SED command in the file, then group these commands using the '{}' character, as shown below: 1,20 {s / [ll] Inux / GNU // Linux / GS / Samba / Samba / GS / POSIX / POSIX / G} The above example will apply three replacement commands to Chain 10 to 20th lines (including these two rows). You can also use rule expressions or combinations of both: 1, / ^ end / {s / [l] INUX / GNU // Linux / GS / Samba / Samba / GS / POSIX / POSIX / GP} This case will All commands between '{}' are applied to the beginning of the line from Chain 1, and the end of the line starting with the letter "end" (if "End" is found in the source file, then the end of the file ends. Addition, insert, and changing rows Since writing a SED script in a separate file, we can use additional, insert, and change the line command. These commands will insert a row after the current line, insert a row before the current row, or replace the current row in the mode space. They can also be used to insert multi-line into the output. Inserting the line command Usage: I / this Line Will BE INSERTED BEFORE Each Line If the address is not specified for this command, then it will be applied to each row and produces the following output: this Line Will Be Inserted Before Each Line Line 1 Herethis Line Will BE Inserted Before Each Line Line 2 Here
This Line Will BE INSERTED BEFORE Each Line Line 3 Here
This Line Will Be Inserted Before Each Line Line 4 Here If you want to insert multiple rows before the current row, you can add additional rows by adding a backslash after the previous row, as follows: I / Insert this line / and this one / And this one / and, uh, this one too. The usage of the additional command is similar, but it will insert one or more lines into the current line in the mode space. The usage is as follows: A / INSERT this LINE After Each Line. Thanks! :) On the other hand, the "change" command will actually replace the current line in the mode space, the usage is as follows: c / you're history, Original Line! Muhahaha! Because of the additional, insert, and changing line commands need to enter multiple lines, you will enter them in a text SED script and then tell SED to execute them by using the '-f' option. There is a problem with other methods to pass commands to SED. Next article, it is also the last article of this SED series, I will demonstrate a lot of excellent instances using SED to complete different types of tasks. I will not only show your script, but also shows why it is. Once you have finished, you will master more knowledge about how to use SED in different projects. see you then! References Read developerWorks Other SED article on Daniel: Universal thread: SED instance, Part 1, and Part 3. View Eric Spement Sed FAQ. Sed 3.02 resources can be found in ftp.gnu.org. A good new Sed 3.02.80 will be found in alpha.gnu.org. In addition, Eric's also has some convenient SED single line programs, and any Sed master with ambition should look at it. If you want to optimize old books, O'Reilly's Sed & AWK, 2nd Edition will be excellent. Maybe read 7th Edition Unix's Sed Man Page (about 1978!). Read the Felix von Leitner short tutorial. Review, discovery, and modify this free DW exclusive tutorial text in Using Regular Expressions. About the author Daniel Robbins live in Albuquerque in New Mexico. He is the founder of Gentoo Technologies, President and CEO, Gentoo Linux and the PC's Advanced Linux and Portage System (next-generation port system for Linux). He is still the author of Macmilla Openlinux Unleashed, SUSE Linux Unleashed and Samba Unleashed author. Daniel from the second grade of elementary school with the computers, then he first touched the LOGO program language and indulge in the PAC-Man game. This may be the reason why he still serves as the chief graphic designer of Sony Electronic Publishing / Psygnosis. Daniel likes to spend time with his wife Mary and newborn daughter Hadassah. Can contact Daniel Robbins via DRobbins@gentoo.org.