Pages

Saturday, October 9, 2010

Simple sed commands

APPEND MATCHED LINE: Add a line following the line matching the regular expression
sed '/regex/a line inserted following regex' file

regex - is any regular expression
a - the append command (can be followed by a space, a \ or directly adjacent to the line)
line inserted following regex - this is the literal text (can include \n if inserting multiple lines)
file - the file to operate on

APPEND FILE: Append lines to the end of a file
sed '$a line of appended text' file

$a - append to the end of file command
line of appended text - this is the literal text (can include \n if inserting multiple lines)
file - the file to operate on

SUBSTITUTE: Substitute text
sed 's/find/replace/g' file

SUBSTITUTE: Substitute advanced
sed 's/\(.*\)\(but.*is\)\(.*\)/\2 : \1\2\3/g' mysql

Escape the parenthesis
\1 \2 \3 are the matches

No comments:

Post a Comment