| Version 2 (modified by , 15 years ago) ( diff ) |
|---|
SysAdmin Tips
Renaming multiple files
for i in *.avi; do j=`echo $i | sed 's/find/replace/g'`; mv "$i" "$j"; done
for i in *.phtml; do mv $i ${i%.phtml}.php; done
Deleting files matching a pattern
find opt/Products -name "*.pyc" -exec rm '{}' \;
Sed: Stream Editor
sed 's/old/new/g' input.txt > output.txt
Faster:
sed '/old/ s/old/new/g' input.txt > output.txt
Do on a folder full of files:
#! /bin/sh # Source files are saved as "filename.txt.bak" in case of error # The '&&' after cp is an additional safety feature for file in *.txt do cp $file $file.bak && sed 's/foo/bar/g' $file.bak >$file done
Insert paragraph:
- e.g. search for lines `#include <termios.h>' and then write:
#ifdef SYSV #include <termios.h> #else #include <sgtty.h> #endif
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:
sed -e '/#include <termios\.h>/{' \
-e 'i\' \
-e '#ifdef SYSV' \
-e 'a\' \
-e '#else\' \
-e '#include <sgtty.h>\' \
-e '#endif' \
-e '}'
1-liners:
Tutorial:
Collected resources:
Attachments (2)
- do_it_with_sed.txt (54.8 KB ) - added by 15 years ago.
- sed1line.txt (18.4 KB ) - added by 15 years ago.
Download all attachments as: .zip
Note:
See TracWiki
for help on using the wiki.

