This is the third day of my participation in Gwen Challenge

Based on arthas 3.5.1, this article introduces the use and implementation of basic commands tee and grep.

A. The tee command

Similar to the traditional tee command, it is used to read data from standard input and output its contents to a file.

The TEE instruction reads data from the standard input device, outputs its contents to the standard output device, and saves them as files.

The parameter name Parameters of the abbreviations Parameters that mandatory The sample
file The file path optional sysprop | tee /path/to/logfile
–append -a Whether to append to a file optional sysprop | tee -a /path/to/logfile

Files at com.taobao.arthas.core.shell.com mand. Internal. TeeHandler. Java.

Inject (List

tokens) SRC/SRC/SRC/SRC/SRC/SRC/SRC/SRC/SRC Inject it into the created teeCommand. And execute the actual implementation logic in the TeeHandler constructor.

Check whether the obtained file is a folder based on the filePath filePath. If yes, an error message is displayed. If the output path does not exist, run new PrintWriter(New BufferedWriter(New FileWriter(file, Append)) to output the content to the file.

2. The grep command

Similar to the traditional grep command.

Files at com.taobao.arthas.core.shell.com mand. Internal. GrepHandler. Java.

The parameter name Parameters of the abbreviations Parameters that mandatory The sample
pattern expression mandatory sysprop | grep java
–regex -e Enable regular expression matching optional
–ignore-case -i Ignore case. Default is case sensitive optional sysprop | grep -i java
–invert-match -v Filter out unmatched rows optional sysprop | grep -v java
–line-number -n According to the number of rows optional sysprop | grep -n java
–trim-end Removes space at the end of each line, enabled by default optional
–before-context -B Displays the line that matches the style, and displays the content before that line (feels the same) optional
–after-context -A Display the content after that line, except for the column that matches the style optional
–context -C Display the content before and after the line, except for the line that matches the style optional
–max-count -m Displays the specified number of rows optional sysprop | grep -m 6 java

Split the retrieved content into newlines. Loop through the array, and if trimEnd is true –trim-end is present, the last blank line of each line will be processed.

AppendLine (StringBuilder Output, int lineNum, int lineNum); String line), using the showLineNumber -n parameter to determine whether to output the first line display plus the number of lines.