Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 3. Perl as a (better) grep comma... > Displaying unmatched records (like g...

3.7. Displaying unmatched records (like grep -v)

Another variation on matching is provided by grep’s v option, which inverts its logic so that records that don’t match are displayed. In Perl, this effect is achieved through conditional printing—by replacing the and print you’ve already seen with or print—so that printing only occurs for the failed match attempts.

The main benefit of this approach is seen in cases where it’s more difficult to write the regex to match the lines you want to print than the ones you don’t. One elementary example is that of printing lines that aren’t empty, by composing a regex that describes empty lines and printing the lines that don’t match:


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint