World's best introduction to sed - good coders code, great reuse |
| World's best introduction to sed Posted: 07 Oct 2011 09:35 AM PDT This is the world's best introduction to sed - the superman of UNIX stream editing. Originally I wrote this introduction for my second e-book, however later I decided to make it a part of the free e-book preview and republish it here as this article. Introduction to sedMastering sed can be reduced to understanding and manipulating the four spaces of sed. These four spaces are:
Think about the spaces this way - sed reads the input stream and produces the output stream. Internally it has the pattern space and the hold buffer. Sed reads data from the input stream until it finds the newline character It's possible to modify the behavior of sed with the Let's look at several examples to understand the four spaces and sed. These are just examples to illustrate what sed looks like and what it's all about. Here is the simplest possible sed program: sed 's/foo/bar/' This program replaces text " abc foo 123-foo-456 Sed opens the file as the input stream and starts reading the data. After reading " Now sed reads in the second line " Now the 3rd line is read in. The pattern space is now " All the lines of the input have been read at this moment and sed exits. The output from running the script on our example file is: abc bar 123-bar-456 In this example we never used the hold buffer because there was no need for temporary storage. Before we look at an example with temporary storage, let's take a look at three command line switches: If you specify sed -n 's/foo/bar/' Then sed will no longer print the pattern space when it reaches the end of the script. So if you run this program on our sample file above, there will be no output. You must use sed's sed -n 's/foo/bar/; p' As you can see, sed commands are separated by the sed -n -e 's/foo/bar/' -e 'p' It's the same as if you used Here is an example. Suppose you have a file called " pkrumins:hacker esr:guru rms:geek And you wish to replace the " sed -i 's/:/;/' users It will silently execute the Actually, before we look at the hold buffer, let's take a look at addresses and ranges. Addresses allow you to restrict sed commands to certain lines, or ranges of lines. The simplest address is a single number that limits sed commands to the given line number: sed '5s/foo/bar/' This limits the The addresses can be also inverted with the sed '5!s/foo/bar/' The inversion can be applied to any address. Next, you can also limit sed commands to a range of lines by specifying two numbers, separated by a comma: sed '5,10s/foo/bar/' In this one-liner the sed -n '5,10p' This will execute the There is a special address sed -n '$p' As you can see, the Next, there is also a single regular expression address match like this sed -n '/a\+b\+/p' Here the sed -rn '/a+b+/p' This way you don't need to quote meta-characters like There is also an expression to match a range between two regexes. Here is an example, sed '/foo/,/bar/d' This one-liner matches all lines between the first line that matches " Now let's take a look at the hold buffer. Suppose you have a problem where you want to print the line before the line that matches a regular expression. How do you do this? If sed didn't have a hold buffer, things would be tough, but with hold buffer we can always save the current line to the hold buffer, and then let sed read in the next line. Now if the next line matches the regex, we would just print the hold buffer, which holds the previous line. Easy, right? The command for copying the current pattern space to the hold buffer is sed -n '/regex/{x;p;x}; h' It works this way - every line gets copied to the hold buffer with the Also notice the command grouping. Several commands can be grouped and executed only for a specific address or range. In this one-liner the command group is Note that this one-liner doesn't work if it's the first line of the input matches sed -n '/regex/{x;1!p;x}; h' Notice the Well, that's it! I think this introduction explains the most important concepts in sed, including various command line switches, the four spaces and various sed commands. If you wish to learn more, I suggest you get a copy of my "Sed One-Liners Explained" e-book. The e-book contains exactly 100 well-explained one-liners. Once you work through them, you'll have rewired your brain to "think in sed". In other words, you'll have learned how to manipulate the pattern space, the hold buffer and you'll know when to print the data to get the results that you need. Have fun! |
| You are subscribed to email updates from good coders code, great reuse To stop receiving these emails, you may unsubscribe now. | Email delivery powered by Google |
| Google Inc., 20 West Kinzie, Chicago IL USA 60610 | |
No comments:
Post a Comment
Keep a civil tongue.