因為awk處理文件是一行一行的處理,所以當內存空間不足時awk可以處理大文件,而cat命令不能處理一個超過內存大小的文件,因為cat是一次性全部讀取文件里面的內容。
下面用實驗證明這個結論:
free 查看內存使用的命令
-m 以M為單位顯示內存的使用情況
-h 以人類能夠讀的懂的格式顯示
B = bytes K = kilos M = megas G = gigas
T = teras
P = petas
[root@localhost 75]# free -mtotal used free shared buff/cache available
Mem: 972 176 110 12 684 621
Swap: 2047 1 2046
[root@localhost 75]#
awk分隔符、Mem: memory 內存
total 是總的物理內存(內存條的大小)
used 使用了多少內存
free 剩余多少內存
shared 共享內存消耗的空間 --》進程和進程之間通信
buff/cache --》buffer cache 緩存
buffer : data from memory to disk
cache : data from disk to memoryavailable: 可用的內存空間
一個新的進程它可以使用的內存的空間=free + buff和cache里的可用
緩存的使用空間,可以釋放的
awk使用linux命令,告訴內核去釋放緩存的空間:
[root@localhost 75]# echo 3 >/proc/sys/vm/drop_caches
[root@localhost 75]# free -htotal used free shared buff/cache available
Mem: 972M 144M 773M 10M 54M 725M
Swap: 2.0G 3.5M 2.0G
/proc 是內核文件系統,內核是一個軟件,控制操作系統的硬件,管理cpu,內存,磁盤,網卡等硬件
process
/proc文件系統 消耗也是內存的空間
sys 系統system
vm virtual memory 虛擬內存=物理內存+交換分區
物理內存:8G
10G
交換分區:swap 20G 從磁盤里劃分出來的一塊空間,用來當作內存使用 速度比較慢
將不活躍的進程臨時存放到交換分區(冷宮)
page in
page out
查看系統給定的內存空間
# 當內存空間剩余30%時,內存空間不足
[root@lier 705]# cat /proc/sys/vm/swappiness
30
內核參數優化,提升進程使用的效率,告訴系統盡可能多使用物理內存,物理內存速度快。
linux里awk?系統開機啟動的時候,內核會讀取這個配置文件,進行相關參數的設置/etc/sysctl.conf
root@aliyun-sz:~# cat /etc/sysctl.conf
vm.swappiness = 0
kernel.sysrq = 1
net.ipv4.neigh.default.gc_stale_time = 120# see details in https://help.aliyun.com/knowledge_detail/39428.html
net.ipv4.conf.all.rp_filter = 0
net.ipv4.conf.default.rp_filter = 0
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_announce = 2# see details in https://help.aliyun.com/knowledge_detail/41334.html
net.ipv4.tcp_max_tw_buckets = 5000
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_syn_backlog = 1024
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_slow_start_after_idle = 0root@aliyun-sz:~#
使用top命令動態監控系統狀態
[root@lier 705]# top
top - 05:03:34 up 1 day, 9:06, 4 users, load average: 0.01, 0.03, 0.05
Tasks: 113 total, 1 running, 112 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.0 us, 0.3 sy, 0.0 ni, 99.5 id, 0.0 wa, 0.0 hi, 0.2 si, 0.0 st
[root@localhost 75]# time cat sc_bigfile.txt |wc -l
43017128real 0m3.595s
user 0m0.495s
sys 0m2.927s
[root@localhost 75]# time tail -1 sc_bigfile.txt
username feng43017128 uid:19509real 0m0.002s
user 0m0.001s
sys 0m0.001s
[root@localhost 75]#
# 連續的空格和:當作一個分割符號
[root@localhost 75]# awk -F"[ :]+" '{print $2,$4}' sc_bigfile.txt
# tr -s " " 將連續的空格壓縮成一個空格
[root@localhost 75]# head sc_bigfile.txt |tr -s " "|awk -F"[ :]" '{print $1,$4}'
substr是awk語言中的一個內置函數
substr(s, i [, n]) Return the at most n-character substring of s starting at i. If n is omitted, use the rest of s.
shell腳本awk,s是指定的字段,i是從s字段的第i個開始數,n表示n個
[root@localhost 75]# cat test.txt2022-7-1 00:01:01 782022-7-1 00:01:04 892022-7-1 00:03:01 1782022-7-1 00:03:05 8902022-7-2 00:03:01 1782022-7-3 00:03:05 8902022-7-30 00:03:01 1782022-7-31 00:07:05 8900
# 截取第一個字段的從第1個字符開始數4個
[root@localhost 75]# cat test.txt |awk '{print substr($1,1,4)}'
2022
2022
2022
2022
2022
2022
2022
2022
# 截取第一個字段的從第6個字符開始數1個
[root@localhost 75]# cat test.txt |awk '{print substr($1,6,1)}'
7
7
7
7
7
7
7
7
練習
算出2022年7月份里的每一個分鐘的流量,具體日志文件格式如下:
2022-7-1 00:01:01 78
2022-7-1 00:01:04 89
2022-7-1 00:03:01 178
2022-7-1 00:03:05 890
2022-7-2 00:03:01 178
2022-7-3 00:03:05 890
…
2022-7-30 00:03:01 178
2022-7-31 00:07:05 8900
[root@localhost 75]# awk '{time[$1,substr($1,5,1),substr($2,1,5)]+=$3}END{for (i in time)print i,time[i]}' test.txt |sort -n -k 3 -t -
2022-7-1-00:01 167
2022-7-1-00:03 1068
2022-7-2-00:03 178
2022-7-3-00:03 890
2022-7-30-00:03 178
2022-7-31-00:07 8900
[root@localhost 75]#
time[$1,substr($1,5,1),substr($2,1,5)]+=$3 表示將年月日和小時分鐘(substr($2,1,5))拼接作為key,流量($3)作為value,將每分鐘的流量相加再用for循環輸出統計后的每分鐘的流量
linux命令速查手冊,統計用戶uid大于1000并且shell是使用bash的用戶的數量
[root@localhost 75]# awk -F: 'BEGIN{num=0}$3>1000 && $NF ~ /bash/{print $0;num++}END{print num}' /etc/passwd
bashzhang:x:1004:1004::/home/bashzhang:/bin/bash
liu1:x:1006:1006::/home/liu1:/bin/bash
zhangliu1:x:1007:1007::/home/zhangliu1:/bin/bash
liu:x:1008:1008::/home/liu:/bin/bash
fengdeyong:x:1011:1011::/home/fengdeyong:/bin/bash
5
awk中的字段求和(累加)
[root@localhost lianxi]# cat grade.txt
id name chinese english math
1 cali 80 80 80
2 tom 90 90 70
3 jarry 70 100 9012 cali 80 80 80
11 tom 90 90 70
13 jarry 70 100 90
[root@localhost lianxi]# awk 'NR>1{sum+=$3}END{print sum}' grade.txt
480
[root@localhost lianxi]# awk 'BEGIN{sum=0}NR>1{sum+=$3}END{print sum}'grade.txt
480
awk的內置函數length,substr
統計沒有設置密碼的用戶的數量,同時輸出用戶名
/etc/shadow
[root@localhost lianxi]# awk -F: 'length($2)<=2 {print $1;sum++}END{print sum}' /etc/shadow
linux shell?統計沒有設置密碼的用戶的數量,同時輸出用戶名前2個字符
[root@localhost lianxi]# awk -F: 'length($2)<=2 {print substr($1,1,2);sum++}END{print sum}' /etc/shadow
練習
1.只顯示df -h結果的第一列文件系統
[root@lier 705]# df -h|awk '{print $1}' 文件系統 devtmpfs tmpfs tmpfs tmpfs /dev/mapper/centos-root /dev/sda1 tmpfs
2.顯示passwd文件的第5行和第10行的行號和用戶名
[root@lier 705]# awk -F: 'NR==5 || NR==10 {print NR,$1}' /etc/passwd 5 lp 10 operator
linux基本命令的使用,3.使用NF變量顯示passwd文件倒數第二列的內容
[root@lier 705]# awk -F: '{print $(NF-1)}' /etc/passwdxxxxxxxxxx awk -F: '{print $(NF-1)}' /etc/passwd[root@lier 705]# awk -F: '{print $(NF-1)}' /etc/passwd
4.顯示passwd文件中第5到第10行的用戶名
[root@lier 705]# awk -F: 'NR>=5 && NR<=10{print $1}' /etc/passwd lp sync shutdown halt mail operator
5.顯示passwd文件中第7列不是bash的用戶名
[root@lier 705]# awk -F: '$3<1000{print $1}' /etc/passwd
6.顯示passwd文件中行號是5結尾的行號和整行內容
[root@lier 705]# awk -F: 'NR ~/5$/{print NR,$0}' /etc/passwd 5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin 15 systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin 25 chx:x:1004:1004::/home/chx:/bin/bash 35 luoyawei:x:1041:1041::/home/luoyawei:/bin/bash 45 zhaojunjie:x:7797:7797::/home/zhaojunjie:/bin/bash 55 user8:x:7807:7808::/home/user8:/bin/bash 65 user18:x:7817:7818::/home/user18:/bin/bash 75 user08:x:7827:7828::/home/user08:/bin/bash 85 lyl:x:10004:10004::/home/lyl:/bin/bash 95 cali10:x:10014:10014::/home/cali10:/bin/bash 105 cali20:x:10024:10024::/home/cali20:/bin/bash 115 mi:x:10034:10034::/home/mi:/bin/bash 125 yueyang:x:10045:10042::/home/yueyang:/bin/bash
linux有什么用?7.用ifconfig/ip add 只顯示ip
yum install net-tools -y -->安裝ifconfig命令的軟件[root@lier 705]# ifconfig|awk 'NR==2{print $NF}' 192.168.1.255
8.ifconfig 命令后使用awk顯示ens33的入站流量和出站流量(字節)
[root@lier 705]# ifconfig|awk 'NR==5 || NR==7{print $1,$5}' RX 336596366 TX 1445420475
9.使用awk命令統計以r開頭的用戶數目,顯示如下效果
[root@lier 705]# awk 'BEGIN{num=0}/^r/{num+=1}END{print num}' /etc/passwd 1
10.顯示每隔2秒的流量的變化
[root@lamp-test ~]# watch -n 2 -d “ifconfig|awk ‘NR==5{print $5}’”
11.統計/etc/passwd文件里以r開頭的用戶的數量,并且顯示出用戶名[root@lier 705]# awk -F: 'BEGIN{num=0}/^r/{print $1;num+=1}END{print num}' /etc/passwd root 1
linux grep?
[root@lier 705]# echo |awk -F: -v username=$name '{print username}'
mengmeng
[root@lier 705]# ls |awk -F: -v username=$name '{print username}'
mengmeng
mengmeng
[root@lier 705]# awk -F: "\$1~/$mn/{print \$0}" /etc/passwd
mengmeng:x:10049:10049::/home/mengmeng:/bin/bash
mengmeng1:x:10050:10050::/home/mengmeng1:/bin/bash
mengmeng12:x:10051:10051::/home/mengmeng12:/bin/bash
單分支
[root@lier 705]# cat /etc/passwd|awk -F: 'length($1)==3{print $0}'
bin:x:1:1:bin:/bin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
lwq:x:1002:1002::/home/lwq:/bin/bash
chx:x:1004:1004::/home/chx:/bin/bash
lqt:x:1010:1010::/home/lqt:/bin/bash
lyl:x:10004:10004::/home/lyl:/bin/bash[root@lier 705]# cat /etc/passwd|awk -F: '{if (length($1)==3) print $0}'
bin:x:1:1:bin:/bin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
lwq:x:1002:1002::/home/lwq:/bin/bash
chx:x:1004:1004::/home/chx:/bin/bash
lqt:x:1010:1010::/home/lqt:/bin/bash
lyl:x:10004:10004::/home/lyl:/bin/bash
雙分支
[root@lier 705]# cat /etc/passwd|awk -F: '{if (length($1)==3) print $0;else print $1}'
多分支
找出超級用戶、系統用戶、普通用戶
awk -F: '{if ($3==0) print $1,"為超級用戶";else if($3>0 && $3<1000) print $1,"為系統用戶";else print $1,"為普通用戶"}' /etc/passwd
找出超級用戶、系統用戶、普通用戶并統計用戶數量
[root@lier 705]# awk -F: '{if ($3==0) {print $1,"為超級用 戶";num1++;}else if($3>0 && $3<1000) {print $1,"為系統用戶";num2++;}else {print $1,"為普通用戶";num3++}}END{print " 超級用戶的數量是:"num1,"系統用戶的數量:"num2,"普通用戶的數量:"num3}' /etc/passwd
[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-DkZcYT5S-1657026505736)(C:\Users\lier\AppData\Roaming\Typora\typora-user-images\image-20220705165522617.png)]
[root@lier 705]# awk -F: '{user[$1]=$3}END{for (i in user) print user[i]}' /etc/passwd
練習
練習: 對nginx的日志文件access.log進行分析,分析出單個ip地址累計下載獲取的文件大小的總數(對每次訪問數據的大小進行求和),顯示下載總數最大的前100個ip地址和下載文件大小,按照下載文件大小的降序排列
[root@lier 705]# awk '{access[$1]+=$10}END{for (i in access) print i,access[i]}' access.log |sort -k 2 -n -r|head -100 result.txt
以下是nginx日志的字段含義
timeiso8601∣time_iso8601|timei?so8601∣host|httpcfconnectingip∣http_cf_connecting_ip|httpc?fc?onnectingi?p∣request|status∣status|status∣body_bytes_sent|httpreferer∣http_referer|httpr?eferer∣http_user_agent
-------------------------------------
2019-04-25T09:51:58+08:00|a.google.com|47.52.197.27|GET /v2/depth?symbol=aaa HTTP/1.1|200|24|-|apple
2019-04-25T09:52:58+08:00|b.google.com|47.75.159.123|GET /v2/depth?symbol=bbb HTTP/1.1|200|407|-|python-requests/2.20.0
2019-04-25T09:53:58+08:00|c.google.com|13.125.219.4|GET /v2/ticker?timestamp=1556157118&symbol=ccc HTTP/1.1|200|162|-|chrome
2019-04-25T09:54:58+08:00|d.shuzibi.co|-||HEAD /justfor.txt HTTP/1.0|200|0|-|-
2019-04-25T09:55:58+08:00|e.google.com|13.251.98.2|GET /v2/order_detail?apiKey=ddd HTTP/1.1|200|231|-|python-requests/2.18.4
2019-04-25T09:56:58+08:00|f.google.com|210.3.168.106|GET /v2/trade_detail?apiKey=eee HTTP/1.1|200|24|-|-
2019-04-25T09:57:58+08:00|g.google.com|47.75.115.217|GET /v2/depth?symbol=fff HTTP/1.1|200|397|-|python-requests/2.18.4
2019-04-25T09:58:58+08:00|h.google.com|47.75.58.56|GET /v2/depth?symbol=ggg HTTP/1.1|200|404|-|safari
2019-04-25T09:59:58+08:00|i.google.com|188.40.137.175|GET /v2/trade_detail?symbol=hhh HTTP/1.1|200|6644|-|-
2019-04-25T10:01:58+08:00|j.google.com|2600:3c01:0:0:f03c:91ff:fe60:49b8|GET /v2/myposition?apiKey=jjj HTTP/1.1|200|110|-|scan
1、計算每分鐘的帶寬(body_bytes_sent)
[root@lier 705]# awk -F"|" '{flow[substr($1,1,16)]+=$(NF-2)}END{for (i in flow) print i,flow[i]}' nginx.log 2019-04-25T10:01 110 2019-04-25T09:56 24 2019-04-25T09:57 397 2019-04-25T09:58 404 2019-04-25T09:59 6644 2019-04-25T09:51 24 2019-04-25T09:52 407 2019-04-25T09:53 162 2019-04-25T09:54 0 2019-04-25T09:55 231
2、統計每個URI(即不帶問號?前面的內容)的每分鐘的頻率
/v2/myposition?apiKey=jjj 中的/v2/myposition[root@lier 705]# awk -F"[| ?]+" '{flow[substr($1,1,16)$5]+=1}END{for (i in flow) print i,flow[i]}' nginx.log 2019-04-25T09:55/v2/order_detail 1 2019-04-25T10:01/v2/myposition 1 2019-04-25T09:59/v2/trade_detail 1 2019-04-25T09:53/v2/ticker 1 2019-04-25T09:58/v2/depth 1 2019-04-25T09:57/v2/depth 1 2019-04-25T09:52/v2/depth 1 2019-04-25T09:51/v2/depth 1 2019-04-25T09:56/v2/trade_detail 1 2019-04-25T09:54/justfor.txt 1
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态