Sed - An Introduction and Tutorial by Bruce Barnett
This illustrates why?sed?scripts get the reputation for obscurity. I could be perverse and show you the example that will search for all lines that start with a "g," and change each "g" on that line to an "s:" sed '/^g/s/g/s/g' Adding a space and using an underscore after the substitute command makes this?much?easier to read: sed '/^g/ s_g_s_g' Er,I take that back. It's hopeless. There is a lesson here: Use comments liberally in a?sed?script under SunOS. You may have to remove the comments to run the script under a different operating system,but you now know how to write a?sed?script to do that very easily! Comments are a Good Thing. You may have understood the script perfectly when you wrote it. But six months from now it could look like modem noise. You can specify a range on line numbers by inserting a comma between the numbers. To restrict a substitution to the first 100 lines,you can use: sed '1,100 s/A/a/' If you know exactly how many lines are in a file,you can explicitly state that number to perform the substitution on the rest of the file. In this case,assume you used?wc?to find out there are 532 lines in the file: sed '101,532 s/A/a/' An easier way is to use the special character "$," which means the last line in the file. sed '101,$ s/A/a/' The "$" is one of those conventions that mean "last" in utilities like?cat -e,?vi,and?ed. "cat -e" Line numbers are cumulative if several files are edited. That is, sed '200,300 s/A/a/' f1 f2 f3 >new is the same as cat f1 f2 f3 | sed '200,300 s/A/a/' >new You can specify two regular expressions as the range. Assuming a "#" starts a comment,you can search for a keyword,remove all comments until you see the second keyword. In this case the two keywords are "start" and "stop:" sed '/start/,/stop/ s/#.*//' 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' |