Refreshing my memory on sed caused me to run into two issues tonight. Firstly…. the -n parameter only shows the patterns that you wish to show [after it is used]. Secondly, the order of deleting and printing lines matters. It turns out that it matters a lot.
Lets say you have a file named contents. It contains:
Gooogle GooooogleBot Gooooogle Pictures Google Plus Reddit Yahoo
Let’s assume that you wanted to just show all lines that contained “Gooogle” [and its similar brothers] with sed. You would write a line that contains this:
sed -e '/Goo[o]\+gle/p' content
Right? Nope. It’ll show all of the items, despite that you used the print command to display items that matched that pattern. To fix this, put in the -n option before -e.
That’s great… But that returns: Gooogle, GooooogleBot, and Gooooogle Pictures. In this example, we don’t like GoogleBot. So lets remove it. You may now write something like:
sed -n -e '/Goo[o]\+gle/p' -e '/Goo[o]\+gleBot/d' content
It seems like a logical extension. Right? The next regular expression should pass over the printed lines left and make an evaluation. Nope, it doesn’t. It’ll display the same results prior to the second expression. What’s going on? Its not a bad expression. Its not a bad command. It’s due to the placement of the prints, deletes, and where you ask that the pattern space be shown. This is some odd quirk, that I haven’t found an explanation for [yet]. But what it turns out to be the correct way of doing it is to rearrange everything where the deletes are first, and then the prints occur [Also, to refuse to print the pattern space after the deletes (weird I know).
So the correct form is:
sed -e '/Goo[o]\+gleBot/d' -n -e '/Goo[o]\+gle/p' content
Bizarre? Yes, very much so, but it works.