sed []
-n] [-e script [-e script]] [-f sfile] [file ...
sed copies file† to STDOUT, editing it according to a script of commands.
Examples
sed "s/input/output/" infile > outfile
sed -e "s/old/older/" -e "s/new/old" needsMultipleChanges.txt
sed -f bunch-o-cmds.sed dafile otherfile > da-SEDed-file
sed "s/;/;\n/" > myprog.c make multi-statment-lines be on seperate lines)
Warning: sed "s/xx/yy/" file > file results in an empty file!
note: sed "s/xx/yy/" file
doesn't update file, rather outputs to STDOUT i.e terminal
Normally, sed compiles the entire script then cycles through the input :
WORK† area (unless there is something left after a D ),
\n
address select the WORK area , and
WORK area to standard output†
including a \n
A script consists of editing commands, one per line:
[address[,address] ]function[arguments]
ed which expects %). s/xxx/yyy/
1 i <table> first line ( it seems that functions that insert should be before functions that reference the entire file.
324 s/xxx/yyy/ A particular line by number (looks dangereous).
. [dot] current line
$ a </table> last line of input
.+1 s/"/'/ on the next line
addresses select the inclusive lines)
/PRE/,/\/PRE/ |
If the second address is BEFORE the first address, only the line at the
first address is selected.
In a context address, the construction
\?regular expression?
where ? is any character, is identical to
/regular expression/
So in this context address \xabc\xdefx ,
the second x stands for itself,
i.e. the regular expression is
abcxdef.
Use a ! at the end of the address range to select all lines except those that match.
A function includes
substitute, append,
change, delete, and others
Some of the functions use a HOLD area to save all or part of
the WORK area for subsequent retrieval.arguments depend on the function
Example:
change
Dec 31 19:37
to 19:37 Dec 31
| command | results | |||||||
|---|---|---|---|---|---|---|---|---|
s/... .. ..:../& &/ | duplicates the entire "date time" string | Dec 31 19:37 Dec 31 19:37
|
Change 123,456 to 123456
substitute from 1 to 3 characters followed by a comma to the same 1 to 3 characters comma and a zz
substitute ,zz to null
| s/.\{1,3\},/&zz/ ; s/,zz// |
\n, matches a new-line embedded in WORK.
A period . matches any character except the terminal new-line of WORK.
Use the negation function ! to
have commands applied only to non-selected WORK areas .
Backslashes in text are treated like backslashes in the replacement
string of an s command, and may be used to protect initial blanks and
tabs against the stripping that is done on every script line.
command list example:
s/xxx/yyy/ { n b top } |
Not all versions of sed will handle [tab]s as \t
Pre process files TRanslating tabs to spaces;
tr '\t' ' ' < $1 |sed -f ~/xml.sedor
y/tab/ /
On single-line patterns, . (dot) matches character.
It does not match the [NewLine] at the end of the line because the newline is removed when the line is put into the work space.
sed adds a newline when the work space is output.
On multi-line patterns obtained with N or G , dot matchs a newline in the middle of the pattern space.
If there are 3 lines in the pattern space, s/.*// deletes all 3 lines.
Example using of a "bracketed expression" character classes s/[[:digit:]]/N/
see awk, ed, grep(includes regular expression discussion), environ
9 May 10, copy pasted list from a PDF and ended up with x'EF 82 B7 ' . Only way I could get them out was to copy the file and
delete everything except the x'EF 82 B7 ' then prefix it with s/ and suffix it with /zzz/g
Gotta be a better way
Modified to Real HTML by Dennis German 12 Mar 2002 see also sed-3.02, see also sedFAQ.html, sed-gnu
Some information taken from SunOS 5.4 man page of 14 Sep 1992