sed (Stream EDitor) refers to a Unix utility for parsing text files and the programming language it uses to apply textual transformations to a sequential stream of data. It reads input files line by line, applying the operation which has been specified via the command line (or a sed script), and then outputs the line. Getting started with sed can be a real pain if you are unfamiliar with perl for regular expressions. Here are a several sed 'one liners' that I use all the time for test file manipulation. More can be found here.
FILE SPACING:
sed '/^$/d;G'
sed 'n;d'
NUMBERING:
sed = filename | sed 'N; s/^/ /; s/ *(.{6,})n/1 /'
sed '/./=' filename | sed '/./N; s/n/ /'
sed -n '$='
TEXT CONVERSION AND SUBSTITUTION:
sed 's/.$//'
sed 's/$'"/`echo r`/"
sed 's/^[ t]*//'
sed 's/[ t]*$//'
sed 's/^[ t]*//;s/[ t]*$//'
sed 's/foo/bar/' # replaces only 1st instance
sed 's/foo/bar/4' # replaces only 4th instance
sed 's/foo/bar/g' # replaces ALL instances
sed 's/scarlet/red/g;s/ruby/red/g;s/puce/red/g'
SELECTIVE PRINTING OF CERTAIN LINES:
sed 10q
sed q
sed -e :a -e '$q;N;11,$D;ba'
sed '$!N;$!D'
sed -n '$p'
sed -n '8,12p'
sed '/regexp/!d'
sed '/AAA/!d; /BBB/!d; /CCC/!d'
sed '/AAA.*BBB.*CCC/!d'
SELECTIVE DELETION OF CERTAIN LINES:
sed '$!N; /^(.*)n1$/!P; D'
sed -n 'G; s/n/&&/; /^([ -~]*n).*n1/d; s/n//; h; P'
sed '$!N; s/^(.*)n1$/1/; t; D'
sed '1d'
sed '1,10d'
sed '$d'
sed 'N;$!P;$!D;$d'
sed -n -e :a -e '1,10!{P;N;D;};N;ba'
sed '/pattern/d'
sed '/^$/d'
sed -e :a -e 's/<[^>]*>//g;/
Popular
-
For all those people who did a clean install of Snow Leopard and do not want to restore any data from Time Machine, you may find manually re...
-
As of Fedora 6 rt-3.6.3 has been available as an RPM. The Fedora RPM has since been rebuilt for Rhel5 using the rt-3.6.6 source code. This d...
-
I recently found it necessary to list all installed Perl modules on one of my linux servers. I wouldn't have thought that it would be as...
-
I have a hard time remembering this stuff, I'm spoiled by GUI tools. For anyone who needs it I posted some mysql cli stuff here. Nothing...
-
I found this out a while ago when looking for info on reading .DMG files on Windows or Linux boxes. Long story short, its a very painful pro...
-
sed (Stream EDitor) refers to a Unix utility for parsing text files and the programming language it uses to apply textual transformations to...
-
Many months after building the RT 3.6.6 rpm repository, I finally got around to gathering up all the required perl modules (in rpm format) f...
-
As a Mac user of less than a year I can sympathize with how painful a switch can be. Many of the keyboard shortcuts I lived by under the tyr...