Stream Editor
Simple usage of SED
sed[options] sed cmds files
Option –e Connect multiple Edit commands
-f Specifies the SED script file name
-N prevents input lines from being entered automatically
Sed ' 4, $d ' test.in the first 4 lines of the output test.in file
Sed ' 3q ' test.in exit before 3 lines output
Sed ' s/public/public ' test.in replace the public word in the file with uppercase
How to locate addresses in Sed_cmd
N |
Represents the nth row |
$ |
Represents the last line |
M,n |
Represents rows from M to n |
/pattern/ |
Queries the rows that contain the specified pattern, such as/disk/,/[a-z]/ |
/pattern/, N |
Represents rows from pattern matching to nth row |
n,/pattern/ |
Represents rows from nth to rows that contain a specified pattern match |
/mode 1/,/mode 2/ |
Represents a row from the containing pattern 1 to the containing pattern 2 |
! |
Reverse selection, indicating m,n! , then take the opposite result of m,n |
Sed–n ' 3,5 ' test.in//Print line 3rd to 5th
Sed–n '/hello/p ' test.in//print out lines containing hello
Sed–n '/hello/= ' test.in//print out line numbers with Hello
Sed–n '/hello/d ' test.in//delete the line containing Hello
Sed Common editing commands
The SED replace command uses the format
"Address" s/old word/new word/"GPW"
Address: If omitted, indicates that all rows are edited
G: Global substitution, default means replacing only the first word to match
P: Print the modified line
W fname: Writes the replaced line contents to the specified file
Sed–n ' S/NORTH/NORTH/GP ' test.in
Sed–n ' s/north/north/w data ' test.in
Sed ' s/[0-9][0-9]$/&.5/' datafile
-n means blocking automatic output
Shell Scripting Learning