| 1 | == Sed: Stream Editor == |
| 2 | Search/Replace: |
| 3 | {{{ |
| 4 | sed 's/old/new/g' input.txt > output.txt |
| 5 | }}} |
| 6 | Faster: |
| 7 | {{{ |
| 8 | sed '/old/ s/old/new/g' input.txt > output.txt |
| 9 | }}} |
| 10 | |
| 11 | Do on a folder full of files: |
| 12 | {{{ |
| 13 | #! /bin/sh |
| 14 | # Source files are saved as "filename.txt.bak" in case of error |
| 15 | # The '&&' after cp is an additional safety feature |
| 16 | for file in *.txt |
| 17 | do |
| 18 | cp $file $file.bak && |
| 19 | sed 's/foo/bar/g' $file.bak >$file |
| 20 | done |
| 21 | }}} |
| 22 | |
| 23 | Insert paragraph: |
| 24 | * e.g. search for lines `#include <termios.h>' and then write: |
| 25 | {{{ |
| 26 | #ifdef SYSV |
| 27 | #include <termios.h> |
| 28 | #else |
| 29 | #include <sgtty.h> |
| 30 | #endif |
| 31 | }}} |
| 32 | |
| 33 | Now, for writing the same script on one line, the -e mechanism is needed... what follows each -e can be considered as an input line from a sed script file, so nothing kept us from doing: |
| 34 | {{{ |
| 35 | sed -e '/#include <termios\.h>/{' \ |
| 36 | -e 'i\' \ |
| 37 | -e '#ifdef SYSV' \ |
| 38 | -e 'a\' \ |
| 39 | -e '#else\' \ |
| 40 | -e '#include <sgtty.h>\' \ |
| 41 | -e '#endif' \ |
| 42 | -e '}' |
| 43 | }}} |
| 44 | |
| 45 | 1-liners: |
| 46 | * http://www.student.northpark.edu/pemente/sed/sed1line.txt |
| 47 | |
| 48 | Tutorial: |
| 49 | * http://sed.sourceforge.net/grabbag/tutorials/do_it_with_sed.txt |
| 50 | |
| 51 | Collected resources: |
| 52 | * http://sed.sf.net/ |
| 53 | |
| 54 | ---- |
| 55 | MaintenanceGuidelines |