1. 查某个时间段的所有的日志
awk '/Start time/,/End time/ ' yourLogName.log
例子如下
awk '/2022-05-23 05:58:59,103/,/2022-05-23 05:59:00,024/ ' yourLogName.log
另外一个例子:它将显示所有匹配Sun或sun的行与匹配Moon或moon的行之间的行,并显示到标准输出上。
awk '/[Ss]un/,/[Mm]oon/ ' myfile
2. 杀掉某些进程
ps -aux|grep 'your key words of ps which will be killed'|awk '{print $2}' | xargs kill
3. 查询Log有多少行
grep 'your log key word‘ test.log|wc -l
4. 查某些方法执行了10000秒,或其它某个时间
log的格式如下
****duration=[465775]***
grep "duration=\[[1-9]\{5,\}\]" test..log
或
grep 'key word' test.log | awk -F 'duration=\\[' '{print $2}' |awk -F ']' 'BEGIN{total=0; num=0} {if ($1>10000) {total+=$1;num++}} END{print num}'
5. 求某方法执行的平均时间。
log的格式如下
****duration=[465775]***
grep 'key word' text.log |awk -F 'duration=\\[' '{print $2}' |awk -F ']' 'BEGIN{total=0;} {total+=$1} END{print total/NR}'
6. 显示文本文件myfile中第七行到第十五行中以字符%分隔的第一字段,第三字段和第七字段
awk -F % 'NR>=7,NR<=15 {print $1 $3 $7}' test.log
7. 查看jar或war包里的是否包含某个文件
zipinfo certainName.war |grep fileName
或
jar -tf certainName.jar |grep fileName
8. 从某个jar或war中删掉,提取或放进去某个文件
Delete file
zip -d fat-aspen-webapp-jobs.jar log4j.properties
Extract file from zip
unzip file.zip file/you/want/to/extract/the_file.txt
Put it back:
zip file.zip file/you/want/to/extract/the_file.txt
9. 查询Server上的内存的使用情况
free -g
结果如下
total used free shared buff/cache available
Mem: 125 85 2 0 37 38
Swap: 31 0 31
注意这是Server层面的上内存使用情况
如果是Top命令,它是进程层面的内存使用情况
top
执行结果
top - 22:52:50 up 15:42, 3 users, load average: 0.87, 1.08, 1.20
Tasks: 1819 total, 6 running, 1813 sleeping, 0 stopped, 0 zombie
%Cpu(s): 5.9 us, 2.6 sy, 0.0 ni, 91.5 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 13114803+total, 2171976 free, 89582576 used, 39393480 buff/cache
KiB Swap: 33554428 total, 33544188 free, 10240 used. 40472352 avail Mem
10. 利用scp传输文件
1、从服务器下载文件
scp username@servername:/path/filename /tmp/local_destination
例如
scp codinglog@192.168.0.101:/home/kimi/test.txt
把 192.168.0.101上的/home/kimi/test.txt 的文件下载到 /tmp/local_destination
2、上传本地文件到服务器
scp /path/local_filename username@servername:/path
例如
scp /var/www/test.php codinglog@192.168.0.101:/var/www/
把本机/var/www/目录下的test.php文件上传到192.168.0.101这台服务器上的/var/www/目录中
3、从服务器下载整个目录
scp -r username@servername:remote_dir/ /tmp/local_dir
例如:
scp -r codinglog@192.168.0.101 /home/kimi/test /tmp/local_dir
4、上传目录到服务器
scp -r /tmp/local_dir username@servername:remote_dir
例如:
scp -r test codinglog@192.168.0.101:/var/www/
把当前目录下的test目录上传到服务器的/var/www/ 目录
其它
1. awk
1.1.awk使用
awk 'BEGIN{ commands } /pattern/ {commands} END{ commands }'
1、BEGIN{ commands }开始块就是在程序启动的时候执行的代码部分,并且它在整个过程中只执行一次。一般情况下,我们可以在开始块中初始化一些变量。
注意:开始块部分是可选的,你的程序可以没有开始块部分。
2、/pattern/ {commands}pattern 部分匹配该行内容成功后,才会执行commands 的内容。
3 、END{ commands }结束块是在程序结束时执行的代码。
注意:结束块部分也是可选的
1.2 awk基本用法
选项 | |
---|---|
-F | -F ',' 或者 -F '正则表达式' -F选项来改变字段分隔符 |
-v | -va=1 赋值一个用户定义变量a的值为1 |
-f | -f scripfile ,从脚本文件中读取awk命令 |
1.3 awk常用系统变量
变量 | 描述(列举常用的,还有很多其他的没怎么常用) |
$n | $1当前记录的第1个字段的内容。和sed中的$1不同,sed表示第一个参数 |
$0 | 整行数据的内容 |
FS | 字段分隔符 (默认是空格) |
OFS | 输出字段的分隔符(默认是空格) |
RS | 行分隔符(默认以\n作为一行的结尾),单行分割成多行用到 |
NR | 行号,从1开始,多文件时候也是连续接着计数 |
FNR | 各文件分别计数的行号,多文件的时候会和NR不同,它会重新计数 NF 一行中字段数量,最后一个字段内容可以用$NF取出 |
ARGC | 命令行参数的数目 |
ARGV | 包含命令行参数的数组,第一个参数是命令awk |
1.3 在awk执行shell command命令,
用system()包含你的命令并且双引号,如果你要用参数$1,$1不能在双引号里。
awk '{system("wc "$1)}' myfile
2. Grep
The grep
command provides many options to change how it searches for patterns or displays results. So far in this article, you've seen some of them. While I can't list all options, here are some other useful examples:
-
Use option
-i
for a case-insensitive search. - Use option
-v
to invert the search and display lines that do not match the pattern. - Use option
-w
to search for entire words only instead of patterns in the middle of other words. - Use option
--color
for colored output, making it easier to spot the matched pattern.
2.1 . Excluding a single pattern
When using the grep command, to invert the search and return lines that do not include a specific pattern or exclude a specific string, you can use the -v flag.
grep -v "pattern" file
Alternatively, if you want to return lines that do not include a specific pattern as a whole, which means a pattern surrounded by non-word characters, such as spaces, punctuation, or the start/end of a line, you can use the -w flag.
grep -v -w "pattern" file
2.2 . Excluding multiple patterns
To return lines that specifically do not include multiple patterns (see grep multiple strings for the opposite), you can use the -e flag combined with the -v flag.
grep -v -e "fist_pattern" -e "second_pattern" file
2.3. 筛选出后缀名是.txt的文件
grep -E '.txt$'