19 June 2015

info

  1. sed short for stream editor

  2. filter and transform text

advanced demo

  1. sed substitution delimiter

    1. @ delimiter

       $ sed 's@/path/to/file@/path/toto/file@g' path.txt
      
    2. / delimiter

       $ sed 's/\/path\/to\/file/\/path\/toto\/file/g' path.txt
      
  2. sed & get matched string

    1. substitute usr/bin to usr/bin/local

       $ sed 's@/usr/bin@&local@g' path.txt
       # & in the replacement part
       # will be replace with `/usr/bin`
      
    2. match the whole line

       # & replaces whatever matches with give regexp
       $ sed 's@^.*$@<<<&>>>@g' path.txt
       # `^.*$` matches the whole line
      
  3. grouping and back-references

    1. get only the first path in each line

       $ sed 's/\(\/[^:]*\).*/\1/g' path.txt
       # `\(\/[^:]*\)` matches the path
       # avaiale before first `:` comes
       # `\1` replaces the first matched group
      
    2. multigrouping

       $ sed '$s@\([^:]*\):\([^:]*\):\([^:]*\)@\3:\2:\1@g' path.txt
      
    3. get the list of usernames in /etc/passwd file

       $ sed 's/\([^:]*\).*/\1/' /etc/passwd
      
    4. parenthesize first character of each word

       $ echo "Welcome To The Geek Stuff" | sed 's/\(\b[A-Z]\)/\(1\)/g'
      
    5. commify the simple numbers

       $ cat numbers
       1234
       12121
       3434
       123
      
       $ sed 's/\(^\|[^0-9.]\)\([0-9]\+\)\([0-9]\{3\}\)/\1\2,\3/g' numbers
      


blog comments powered by Disqus