1. Questions on the face of the awk command
(1) Recently logged in 5 accounts
Last-n 5 | Awk-f ': ' (specify domain split symbol) ' {print $} '
-N indicates number and how many rows need to be displayed. Reads a record with a ' \ n ' newline, then divides the record by the specified domain delimiter, fills the field, and $ $ represents all fields, representing the first field, $n representing the nth field. The default Domain delimiter is the "blank key" or "[tab] key", so the login user, $ $ means the login user IP, and so on.
(2) Count lines of text with awk
awk ' {count++} end{print ' User Count is ', count} '/etc/passwdawk ' end{print ' user Count is ' NR} ' filename
or directly wc-l the file name can also
Grep-o ' This device ID is within the blacklist ' jr-adx-dsp_dspbidding.log.20170626* | Wc-l
There are many others, such as:
(3) test content is as follows:
Zhangsan 80lisi 81.5wangwu 93zhangsan 85lisi 88wangwu 97zhangsan 90lisi 92wangwu 88
Output format required:
name###### #average ###### #total
Zhangsan xxx xxx
Lisi xxx xxx
Wangwu xxx xxx
Where average is the average score and total is the total
awk ' {a[$1]+=$2;b[$1]++}; End{for (i in a) print I,a[i]/b[i],a[i]} ' test.txt
Can be used to index data.
(4) Each level in this folder has an. svn hidden folder that needs to be deleted, using the following command:
$find. -type d-name "*.SVN" | Xargs RM-RF
(5) Count the number of rows starting with P:
Find-name test.sh-type F-print|xargs awk '/^p/{count++}end{print count} '
Using Xargs, you pass the found file name as a parameter to the awk command, and if the output is a line of content, there is no need to add Xargs, as follows:
$cat a.txt |awk ' {print $} '
(6) Extract the word "APA" in the file and output it to a file
Find. -name "*.txt" |xargs grep-oe "[a-za-z]*apa[a-za-z]*" |awk-f: ' {print $} '
The SED command is good for line matching, but with exactly what is being matched from a row, the grep command is used with the-O and-e options to achieve this. Where-o means "only-matching", meaning "match only". Light is not enough, it is powerful to use extended regular expressions with the-e option.
Reference: http://www.cnblogs.com/chengmo/archive/2010/10/10/1847287.html
2. Write a simple shell script
Find out if the file exists under the/root/directory
#/bin/bash //with bash Shell to interpret echo "Enter a file name:" read a //read the user input and assign the user input value to the right variable if test -e/root/$a
// Test Check the file and compare values, note the variable reference with $then echo "The file is exist!" else echo "The file is not exist!" Fi
The format of the If/else condition is as follows:
if[expression] Then command statement block Else command statement block fi
3, Linux System monitoring command
(1) Top command: Linux command. You can view real-time CPU usage. You can also view CPU usage for the most recent period of time.
(2) PS command: Linux command. Powerful process status monitoring commands. You can view the current CPU usage of the process and threads in the process. The sampled data that belongs to the current state.
(3) commands provided by Jstack:java. You can view the current thread stack run of a process. Depending on the output of this command, you can locate the current running state of all threads of a process, run code, deadlock, and so on.
(4) Pstack:linux command. You can view the current thread stack run of a process.
(5) Disk usage: Iostat
Specifically, see article: https://i.cnblogs.com/EditPosts.aspx?postid=5702288&update=1
4, online cpu100%, how to locate and troubleshoot problems
(1)
1.jps getting the PID of the Java process
2.jstack PID >> java.txt export CPU high process thread stack
3.top-h-P PID to see which thread of the corresponding process is consuming high CPU
4.echo "OBASE=16; PID "| BC Converts a thread's PID to 16, uppercase to lowercase
5. In the second step of the exported Java.txt, look for the thread PID converted into 16 binary. Find the corresponding line stacks, and analyze what business operations the high-load line stacks are. Optimize the program and handle the problem.
(2)
1. Use top to navigate to high CPU-intensive process PID and view process-specific information
Top //command gets process-related information and finds threads that consume high CPU PS aux | grep pid//Get specific information about the PID process, such as a tomcat service process
2. Get thread-related information
Several parameters of the PS command:
-M Show All threads
-p PID process uses CPU time
-o user-defined format
Tid thread ID
Two parameters of the sort command:
-n Sort by numeric size
-R sorted in reverse order
3. Convert the required thread ID to 16 binary format
printf "%x\n" tid
4. Stack information for the print thread
Jstack PID |grep tid-a 30
-A 30 matches the following 30 lines of the row
Reference article:
(1) http://www.cnblogs.com/dragonflyyi/p/4343778.html
(2) http://www.cnblogs.com/pangguoping/p/5715848.html
Java Interview 12| Linux and Shell scripts