Sed - An Introduction and Tutorial by Bruce Barnett
|
The first pattern turns on a flag that tells?sed?to perform the substitute command on every line. The second pattern turns off the flag. If the "start" and "stop" pattern occurs twice,the substitution is done both times. If the "stop" pattern is missing,the flag is never turned off,and the substitution will be performed on every line until the end of the file. You should know that if the "start" pattern is found,the substitution occurs on the same line that contains "start." This turns on a switch,which is line oriented. That is,the next line is read and the substitute command is checked. If it contains "stop" the switch is turned off. Switches are line oriented,and not word oriented. You can combine line numbers and regular expressions. This example will remove comments from the beginning of the file until it finds the keyword "start:" sed -e '1,/start/ s/#.*//' This example will remove comments everywhere except the lines?between?the two keywords: sed -e '1,/start/ s/#.*//' -e '/stop/,$ s/#.*//' The last example has a range that overlaps the "/start/,/stop/" range,as both ranges operate on the lines that contain the keywords. I will show you later how to restrict a command up to,?but not including?the line containing the specified pattern. It is in??But I have to cover some more basic principles. Before I start discussing the various commands,I should explain that some commands cannot operate on a range of lines. I will let you know when I mention the commands. In this next section I will describe three commands,one of which cannot operate on a range. Using ranges can be confusing,so you should expect to do some experimentation when you are trying out a new script. A useful command deletes every line that matches the restriction: "d." If you want to look at the first 10 lines of a file,you can use: sed '11,$ d' |

