1. Replace the command s, do not change the original file
|
|
Sed ' s /book/bookall/' mysed
|
|
Sed-n ' s/book/bookall/' mysed
|
-N use alone will not output any content
|
Sed ' s/book/bookall/p ' mysed
|
P is used alone, is the default output
|
Sed-n ' s/book/bookall/p ' mysed
|
-N, P used together, only output changed rows
|
Sed-i ' s/book/bookall/' mysed
|
-I AIX does not support
|
2. G Comprehensive Replacement logo
|
|
Sed ' s /book/bookall/g ' mysed
|
The default is full replacement
|
Sed ' s/book/bookall/ ' mysed
|
Replace from Nth, note that no G
|
/ |
/the definition in SED can also be other such as: or | |
Sed '/\/youbook/mybook/' |
' Internal definition must be escaped |
3. Delete Operation D command |
|
Sed '/^$/d ' mysed |
Delete blank line |
Sed ' 4d ' mysed |
Delete 4th line of file |
Sed ' 2, $d ' mysed |
Delete all rows from 2 to the end of the line |
Sed ' $d ' mysed |
Delete last line |
Sed '/^book/' d mysed or sed '/^book/d ' mysed |
Delete all lines at the beginning of book |
Matched string Tag &
|
|
echo This is my i-learning of sed | Sed ' s/\w\+/[&]/g ' |
Regular expression \w\+ match every word, use "&" to replace it,& each word it matches before (AIX doesn't seem to work)
|
Sed ' s/^book/& of mathbook/' mysed |
Works |
SUBSTRING matching tag \1 |
|
echo This is digit 7 in a number | Sed ' s/digit \ ([0-9]\)/\1/' |
Replace the left command with a matching substring digit 7 is replaced with 7, and the style matches the substring is 7,\ (...) \ used to match substrings, and for the first substring matched to \1, and so on \2 |
echo a B | Sed ' s/\ ([a-z]\) \ ([a-z]\)/\2 \1/' |
A b position swap, a B if it's a word. |
Sed-n ' s/^\ (book\)/\1s/p ' mysed |
Book at start position, marked as \1 |
Combining expressions |
|
Sed ' expression 1 ' | Sed ' expression 2 ' is equivalent to sed ' expression 1; expression 2 ' |
|
Test=hello echo Hello World | Sed "s/$test/hello/" |
sed expressions can be referenced using single quotes, but if an expression contains a variable string inside, you need to use double quotes
|
The range of selected rows, (comma) |
|
Sed-n '/mybook/,/fine/p ' mysed |
All rows in the range determined by the template MyBook and check are printed |
Sed-n ' 5,/^fine/p ' mysed |
Print all rows from line fifth to the first line that contains the beginning of fine |
Sed-n '/mybook/,/fine/s/$/aaa ccc/p ' mysed |
For the line between the template MyBook and the fine, the end of each line is replaced with the string AAA CCC |
Multi-point Edit e command |
|
Sed-e ' 1,5d '-e ' s/book/shitbook/' mysed |
The-e option allows multiple commands to be executed on the same line, and the order in which the commands are executed affects the results |
Read from file into the R command |
|
Sed '/fine/r readfile ' mysed |
Append the content read from the ReadFile to the line behind the fine |
Write File W command |
|
Sed-n '/fine/w WriteFile ' mysed |
Write data from mysed that contains fine rows to WriteFile |
Append (line) a\ command |
|
Sed '/^fine/a\this is me line ' mysed |
Append the This is I line to the lower of the fine start Wokrs |
Sed-i ' 2a\this is my food ' mysed |
Append the This is to the lower of the second row not Wokrs
|
Insert on Row i\ |
|
Sed '/^fine/i\this is your prite ' mysed |