Main index

Introducing UNIX and Linux


Overview
Using filters
      Collating sequence
      Character classes
Character-to-character transformation
Selecting lines by content
      Regular expressions
      Basic regular expressions
      Extended regular expressions
      Grep
Stream editor
      Sed addresses
Splitting a file according to context
Choosing between the three filters
More on Vi
Summary
Exercises

Answer to chapter 10 question 5

Begin by replacing all characters which are not digits by blanks, then translate each blank to a newline, remove all empty lines, and finally sort the result to be in numerical (rather than lexical) order, removing duplicates:

sed 's/[^0-9]/ /g' |    # Pattern [^0-9] matches
                        #      any non-digit
    tr " " "\n"  |      # Replace blanks by newlines
    grep -v '^$' |      # Select all lines NOT
                        #      matching ^$
    sort -u -n          # Sort, remove duplicated lines
                        #      into numerical order

Copyright © 2002 Mike Joy, Stephen Jarvis and Michael Luck