Detailed explanation of linux sed commands and sed commands
Sed is a good file processing tool. It is a pipeline command mainly used for processing by behavior units. It can replace, delete, add, and select data rows, next, let's take a look at the usage of sed.
Sed command line format:
Sed [-nefri] 'command' input text
Common options:
-N: Use silent mode. In general sed usage, all information from STDIN is usually listed on the screen. However, if the-n parameter is added, only the row (or action) that has been specially processed by sed will be listed.
-E: directly edit the sed action in the Command column mode;
-F: Write the sed action directly in a file.-f filename can execute the sed action in filename;
-R: sed supports the syntax of extended regular notation. (The default is the basic regular expression syntax)
-I: directly modify the content of the file to be read, rather than output by the screen.
Common commands:
A: new. a can be followed by strings. These strings will appear in a new row (the next row currently )~
C: replace. c can be followed by strings. These strings can replace rows between n1 and n2!
D: delete. Because it is deleted, d is usually not followed by any comment;
I: insert, I can be followed by strings, and these strings will appear in the new line (the previous line currently );
P: print the selected data. Usually p will work with the sed-n parameter ~
S: replace, you can directly replace the work! Generally, this s action can be combined with regular notation! For example, 1, 20 s/old/new/g!
Example: (assume we have a file named AB)
Delete a row
# Sed '1d 'AB # Delete the first line
# Sed '$ d' AB # Delete the last row
# Sed '1, 2d 'AB # Delete the first row to the second row
# Sed '2, $ d' AB # Delete the second row to the last row
Show a row
. # Sed-n '1p' AB # display the first line
# Sed-n' $ P' AB # display the last line
# Sed-n '1, 2 p 'AB # display the first row to the second row
# Sed-n' 2, $ P' AB # display the second row to the last row
Use mode for query
# Sed-n'/ruby/P' AB # query all rows including the keyword ruby
# Sed-n'/\ $/P' AB # query all rows with the keyword $. Use backslash \ to SHIELD special meanings.
Add one or more strings
# Cat AB
Hello!
Ruby is me, welcome to my blog.
End
# Sed '1a drink tea 'AB # Add the string "drink tea" after the first line"
Hello!
Drink tea
Ruby is me, welcome to my blog.
End
# Sed '1, 3a drink tea 'AB # Add the string "drink tea" from the first row to the third row"
Hello!
Drink tea
Ruby is me, welcome to my blog.
Drink tea
End
Drink tea
# Sed '1a drink tea \ nor coffee 'AB # add multiple lines after the first line and use the line break \ n
Hello!
Drink tea
Or coffee
Ruby is me, welcome to my blog.
End
Replace one or more rows
# Sed '1c Hi' AB # Replace the first line with Hi
Hi
Ruby is me, welcome to my blog.
End
# Sed '1, 2c Hi' AB # Replace the first and second rows with Hi
Hi
End
Replace a part of a row
Format: sed's/string to be replaced/New String/G' (the string to be replaced can use a regular expression)
# Sed-n'/ruby/P' AB | sed's/ruby/bird/G' # Replace ruby with bird
# Sed-n'/ruby/P' AB | sed's/ruby // G' # Delete ruby
Insert
# Sed-I '$ a bye' AB # Enter "bye" in the last line of the file AB"
# Cat AB
Hello!
Ruby is me, welcome to my blog.
End
Bye
Delete matched rows
Sed-I '/match string/d' filename (Note: if the matching string is a variable, you need "" instead ''. Remember)
Replaces a string in the matched row.
Sed-I '/match string/s/replace source string/replace target string/G' filename
Locate logs
During centralized deployment of system applications, many logs are difficult to locate. Obtaining logs for a certain period of time is critical for O & M personnel.
Sed:
Sed-n'/May 20 17/, $ P'/var/log/messages | less
Log of sed truncation time range
First, let's take a look at the log format:
09:25:55, 606 [catalina-exec-74] INFO org. springframework. jdbc. datasource. JdbcTransactionObjectSupport-JDBC 3.0 Savepoint class is available
09:25:55, 658 [catalina-exec-74] WARN org. hibernate. util. JDBCExceptionReporter-SQL Error: 0, SQLState: null
09:25:55, 606 [catalina-exec-74] INFO org. springframework. jdbc. datasource. jdbcTransactionObjectSupport-JDBC 3.0 Savepoint class is available2010-11-17 09:25:55, 658 [catalina-exec-74] WARN org. hibernate. util. JDBCExceptionReporter-SQL Error: 0, SQLState: null
......
Ession for transaction; nested exception is org. hibernate. exception. GenericJDBCException: Cannot open connection
At org. springframework. util. ReflectionUtils. handleReflectionException (ReflectionUtils. java: 58)
At com. lottery. common. action. CommonAction. init (CommonAction. java: 110)
09:28:08, 227 [main] INFO org. springframework. web. filter. CharacterEncodingFilter-Initializing filter 'characterencodingfilter'
Based on the log format, you need to extract the logs between 09:25:55 and 09:28:08 on November 17 ,.
Use the sed command as follows:
Sed-n'/09:25:55/,/09:25:55/p 'logfile
In this way, you can accurately extract logs for a certain period of time.
But the problem arises again. Because the log file is huge, you cannot open the file through vi.
Use a regular expression based on the log format you have seen before.
Sed-n'/2010-11-17 09: [0-9] [0-9]: [0-9] [0-9]/,/2010-11-17 16: [0-9] [0-9]: [0-9] [0-9]/P' logfile
If there is no problem, the above will be able to filter logs for the specified time range.