2008年2月18日星期一

begin sed at a sight

Sed is an Editor..
The normal way to use 'sed' is:
sed 'reg_pattern/commands' filename

sed processes each line in a file as a unit.
sed finds out each line matched the given regular expression, & deal with it by these 'commands'. commands could be a single command such as D, or sequence of commands quoted by '{}'.

the most used format of sed : "sed 's/pattern/replace/' file" is an abbreviation, 's' is a command. searching pattern reg is absent, so that 's' command applies to each line.
the full format is : sed '/.*/{s/pattern/replace/g}' file

there are some usually used examples:

# join all lines
sed ':x;/./{N;s/[\n\r]//g;bx}'

#remove single-line comment
sed 's/\/\*.*\*\///g'

#remove multi-line comment
sed '/^[ \t]*\/\*/, /.*\*\//d'

#remove dos-line-breaker in unix file
sed 's/\cm//g'

没有评论: