wiki:MaintenanceGuidelinesTips

Version 1 (modified by Fran Boon, 15 years ago) ( diff )

--

Sed: Stream Editor

Search/Replace:

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:


MaintenanceGuidelines

Attachments (2)

Download all attachments as: .zip

Note: See TracWiki for help on using the wiki.