|
|
|
|
Sed is a stream editor. A text stream comes in and it looks at lines as they go by and optionally makes changes to the stream. Eventually, another stream comes out which is the original stream plus changes. It's useless as a text file editor but incomparable as a tool to modify individual lines. ExamplesModify a substringIn this example, I had a source management program called p4 which was output a line that needed to be modified. This output was passed into sed which performed the necessary changes. p4 change -o | sed "s/<enter description here>/Daily automatic update/" >c:\temp.txt p4 change -o - this was the source management program command. It wanted to have a label applied to its source change. Normally, this would have resulted in a dialog box that would require manual intervention but the "-o" over rode that and sent the string out on the standard output. | - this is the DOS pipe operator. The standard output was piped to the next program in the command line instead of being displayed on the screen. sed - this told DOS to crank up sed and hijack its standard input. "s/ - this told sed that a search and replace operation was to be performed. The '/', by virtual of the fact that it followed the 's', is the delimiter between the search string and the replace string. <enter description here> - this is the string that is to be searched for. / - Signifies that the search string has ended and that the replacement string is about to start. Daily automatic update - this is the string that will replace all instances of the search string. /" - Signifies the end of the replacement string and of the sed commands as well. >c:\temp.txt - this is the file that will receive the altered results as they come out of sed. Reformat a numberIn this example, I wanted to extract and then sort build times such that the shortest build time came first and the longest build time came last. The problem was that MsDev outputs build times such that leading 0's in the minutes column are suppressed. As a result, sorting was inaccurate as the following two lines show. Build Time 10:11:34 The solution was to use sed to insert leading 0's on all lines that had leading zero's suppressed. This could be done by searching all instances of a space followed by a digit followed by a colon. This result was passed through to sed and a leading 0 was prepended. Assume that Time.txt already has the times in it and it is filled with lines that look like: Spawn Time 0:19:4 sed "s/ \([0-9]\):/ 0\1:/" <Times.txt >FormatedTimes.txt In this case, the search pattern is a space followed by a single digit followed by a colon. The syntax gets clouded because the single digit is flagged for being remembered. By surrounding a term with '\(' and '/)', we're telling sed that this particular term is something that should be remembered. The replacement pattern says to replace the search pattern with a space followed by a zero followed by the first remembered value followed by a colon. In this case, there is only one remembered value per line. As a result of running this command, single digit minutes are turned into double digit minutes in which the first digit is a zero.
|
|
|