Linux Command sed
Sed is a lightweight linux stream editor used for row operations. It is mainly used for data selection, replacement, deletion, and addition.
Permission: All Users
Bit:/bin/sed
Usage: sed [-nefr] [action] file
Parameters:
-N
When-n is not added, both the data processed by sed and the data not processed are output. When-n is added, only the rows operated by sed are output.
-E
Directly edit sed actions in Command Line Mode
Example: sed-e's/aaa/B/g; s/aa/B/G' fileThat is, you can operate multiple actions at the same time. Pay attention to the semicolon in the middle.
-F
Write the sed action directly in a file, and-f file can execute the sed action in filename.
-R
Sed actions support extended regular expressions (basic regular expressions are supported by default)
-I
Directly modify the Read File Content
Action Description: [n1 [, n2] function
Does not necessarily exist. Generally, it indicates the number of rows selected for the action. If the action is replaced between 5 and 15 rows, "10, 20c"
A is added. The next row of the current row
Sed '2a hello world' file insert hello world after 2nd rows
Sed '2a hello world \
My name is csdn 'file
Insert the output lines hello world and my name is scdn.
Pay special attention to "\" after hello world "\"
C replacement, which is the replacement of the whole line
Sed '3, 6c hello csdn 'file
Replace lines 3 to 6 with hello csdn. (lines 3rd to 6 have only one line of hello csdn. Not all lines 3 to 6 are hello csdn)
D. Delete
Sed '3, 6d 'file: output the content after 3 to 6 lines of the file.
I insert, the last row of the current row
Similar to Action a, refer to action a usage.
P print
Sed-n'2, 5' file: print the 2nd to 5 rows of file
S replacement
Sed's/oldString/newString/G' file oldString and newString support basic regular expressions and extended Regular Expressions
Cat-n nginx | sed '1, 99 s/[0-9] \ {1 ,\}\. [0-9] \ {1 ,\}\. [0-9] \ {1 ,\}\. [0-9] \ {1, \} // G' Replace the IP address of the first field from line 1 to line 99 of the nginx log with null
Result:
91 - - [15/Dec/2014:23:00:10 +0800],GET /identified?key=this-is-TMP-apikey&digest=914EBB2FD551844CBE15513C0126E6EE&digest-algorithm=ed2k&hash=9D37BB2988F26973371FAB028FEEEB93D5542300& HTTP/1.1,200 65,-,XMP Windows Client 4.9,-,ed2k://|file|ki141213.wmv|1119747351|914EBB2FD551844CBE15513C0126E6EE|h,00E04C377725NG4E,ki141213.wmv,1119747351 92 - - [15/Dec/2014:23:00:10 +0800],GET /identified? HTTP/1.1,401 112,-,-,-,-,-,-,- 93 - - [15/Dec/2014:23:00:10 +0800],GET /identified? HTTP/1.1,401 112,-,-,-,-,-,-,- 94 - - [15/Dec/2014:23:00:10 +0800],GET /identified? HTTP/1.1,401 112,-,-,-,-,-,-,- 95 - - [15/Dec/2014:23:00:10 +0800],-,400 0,-,-,-,-,-,-,- 96 - - [15/Dec/2014:23:00:10 +0800],GET /identified?key=this-is-TMP-apikey&digest=76BB7C854168DCD1BF15FCB92E4114FFD4CFE7DE&digest-algorithm=magnet&hash=28003498535& HTTP/1.1,200 65,-,Thunder Windows Client 7.9,-,magnet:?xt=urn:btih:76BB7C854168DCD1BF15FCB92E4114FFD4CFE7DE,EC55F9E92A5212LQ,ABP-209R,1067178826 97 - - [15/Dec/2014:23:00:10 +0800],GET /identified? HTTP/1.1,401 112,-,-,-,-,-,-,- 98 - - [15/Dec/2014:23:00:10 +0800],GET /identified? HTTP/1.1,401 112,-,-,-,-,-,-,- 99 - - [15/Dec/2014:23:00:10 +0800],-,400 0,-,-,-,-,-,-,- 100121.12.57.154 - - [15/Dec/2014:23:00:10 +0800],GET /identified? HTTP/1.1,401 112,-,-,-,-,-,-,-
If the preceding command is directly added to-I, you can directly modify the file.