wc -l +文件名 显示文件行数head -5 +文件名 显示文件前5行tail -5 +文件名 显示文件后5行sed -n '5,20p' +文件名 显示文件中间一段
输出固定间隔的数字
seq 0 7 500
打印指定文件第五行
sed -n 5p +文件名
打印空行的行号
awk '/^\s*$/{print NR}' +文件名
去掉空行
awk -n '{if($0 != "")print $0}' + 文件名
统计一个文本文件 nowcoder.txt中字母数小于8的单词
nowcoder.txt 内容如下:
how they are implemented and applied in computer
awk -F" " '{
for(i=1;i<=NF;i++){
if(length($i)<8){
print $i
}
}
}' nowcoder.txt
统计所有进程占用内存大小的和
假设 nowcoder.txt 内容如下:
root 2 0.0 0.0 0 0 ? S 9月25 0:00 [kthreadd]
root 4 0.0 0.0 0 0 ? I< 9月25 0:00 [kworker/0:0H]
web 1638 1.8 1.8 6311352 612400 ? Sl 10月16 21:52 test
web 1639 2.0 1.8 6311352 612401 ? Sl 10月16 21:52 test
tangmiao-pc 5336 0.0 1.4 9100240 238544 ?? S 3:09下午 0:31.70 /Applications
以上内容是通过ps aux | grep -v 'RSS TTY' 命令输出到nowcoder.txt文件下面的
sum=0
for i in `awk '{print $6}' nowcoder.txt`
do
((sum+=$i))
done
echo $sum
统计每个单词出现的个数
示例:
假设 nowcoder.txt 内容如下:
welcome nowcoder
welcome to nowcoder
nowcoder
你的脚本应当输出(以词频升序排列):
to 1
welcome 2
nowcoder 3
awk '{
for(i=1;i<=NF;i++)
a[$i]+=1
}
END{
for(x in a)
print x,a[x]
}' nowcoder.txt
第二列是否有重复
文件内容
20201001 python 99
20201002 go 80
20201002 c++ 88
20201003 php 77
20201001 go 88
20201005 shell 89
20201006 java 70
20201008 c 100
20201007 java 88
20201006 go 97
awk '{a[$2]+=1}END{
for(x in a){
if(a[x]>1)
print a[x],x
}
}' nowcoder.txt
转置文件的内容
nowcoder.txt
job salary
c++ 13
java 14
php 12
awk '{
for(i=1;i<=NF;i++){
if(NR==1){
row[i]=$i;
}else{
row[i]=row[i]" "$i;
}
}
}END{
for(i=1;i<=NF;i++){
print row[i]
}
}' nowcoder.txt
打印每一行出现的数字个数
1、统计一个文本文件 nowcoder.txt中每一行
出现的1,2,3,4,5数字个数
。
2、并且要计算一下整个文档中一共出现了
几个
1,2,3,4,5数字
数字总数
。
nowcoder.txt
a12b8
10ccc
2521abc
9asf
#/bin/bash
linecount=0
sum=0
count=0
while read line
do
for(( i=0;i<${#line};i++ ))
do
if [[ ${line:$i:1} =~ [1-5] ]]
then
count=$(($count+1))
fi
done
linecount=$(($linecount+1))
echo "line$linecount number:$count"
sum=$(($sum+$count))
count=0
done<nowcoder.txt
echo "sum is $sum"
求输入的一个的数组的平均值
第1行为输入的数组长度N
第2~N行为数组的元素,如以下为:
数组长度为4,数组元素为1 2 9 8
示例:
4
1
2
9
8
awk '{if(NR==1){
N=$1
}
else{
sum+=$1
}
} END{printf ("%.3f",sum/N) }'
去掉不需要的单词
写一个 bash脚本以实现一个需求,去掉输入中的含有B和b的单词
示例:
假设输入如下:
big
nowcoder
Betty
basic
test
awk '{
for (i=1;i<=NF;i++)
if ($i ~ /.*[bB]+.*/ )
{
continue
}else {
print $i
}
}'
判断输入的是否为IP地址
awk '{
if($0~/^((25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{0,2})\.){3}(25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{0,2})$/)
print "yes"
else if($0~/^([0-9]*\.){3}[0-9]*$/)
print "no"
else
print "error"
}' nowcoder.txt
去掉所有包含this的句子
假设输入如下:
that is your bag
is this your bag?
to the degree or extent indicated.
there was a court case resulting from this incident
welcome to nowcoder
awk '$0!~/this/ {print $0}'
将文件的每行字段逆序输出
awk -F ":" '{
a[NR]=$NF;
for(i=NF-1;i>0;i--)
a[NR]=a[NR]":"$i}
END{
for(k in a)
print a[k]
}
' nowcoder.txt
域名进行计数排序处理
将域名取出并根据域名进行计数排序处理。
nowcoder.txt
http://www.nowcoder.com/index.html
http://www.nowcoder.com/1.html
http://m.nowcoder.com/index.html
awk -F '/' '{print $3}' nowcoder.txt |sort |uniq -c |sort -r |awk '{print $1" "$2}'
打印等腰三角形
awk 'BEGIN{
for(n=1;n<=5;n++){
row="";
for(i=1;i<=5-n;i++){
row=row " "
}
for(i=1;i<=n;i++){
row=row "*" " "
}
print row
}
}'
打印只有一个数字的行
现在需要你写脚本,打印只有一个数字的行。
假设nowcoder.txt内容如下
haha
1
2ab
cd
77
awk '/(^[0-9]([a-z]+)$)|(^[a-z]*[0-9]$)|(^[a-z][0-9][a-z]$)/'
格式化输出
有一个文件nowcoder.txt,里面的每一行都是一个数字串,假设数字串为“123456789”,那么我们要输出为123,456,789。
假设nowcoder.txt内容如下
1121231234123456
awk '{
l=length($0)
f=l%3
for (i=1;i<=l;i++){
printf substr($0,i,1)
if((i-f)%3==0 && i!=l)
printf ","
}
print ""
}' nowcoder.txt
处理文本
有一个nowcoder.txt,假设里面的内容如下
111:13443222:13211111:13643333:12341222:12123
awk -F ":" '{
a[$1]=a[$1]$2 "\n"
}
END {
for (i in a){
printf("[%s]\n%s",i,a[i])
}
}' nowcoder.txt
nginx的日志我们存储在nowcoder.txt里,
要你统计出2020年4月23号的访问ip次数,并且按照次数降序排序。
192.168.1.20 - - [21/Apr/2020:14:27:49 +0800] "GET /1/index.php HTTP/1.1" 404 490 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0"
192.168.1.21 - - [21/Apr/2020:15:27:49 +0800] "GET /2/index.php HTTP/1.1" 404 490 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0"
........
awk '{
if ($4 ~ /\[23\/Apr\/2020.*/){
res[$1]++;
}
}END{
for(k in res) {
print res[k] " " k
}
}' nginx.txt | sort -nr -k 1 -t " "
nginx日志分析2-统计某个时间段的IP
192.168.1.20 - - [21/Apr/2020:14:27:49 +0800] "GET /1/index.php HTTP/1.1" 404 490 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0"192.168.1.21 - - [21/Apr/2020:15:27:49 +0800] "GET /2/index.php HTTP/1.1" 404 490 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0"
awk '{
if ($0 ~ /23\/Apr\/2020:2[0-2]/){
a[$1]=0
}
}END{
print(length(a))
}' nginxIp.txt
nginx日志分析4-查询某个IP的详细访问情况
192.168.1.20 - - [21/Apr/2020:14:27:49 +0800] "GET /1/index.php HTTP/1.1" 404 490 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0"192.168.1.21 - - [21/Apr/2020:15:27:49 +0800] "GET /2/index.php HTTP/1.1" 404 490 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0"
awk '$1=="192.168.1.22"{a[$7]++}END{for(i in a){print a[i],i}}' | sort -r
nginx日志分析5-统计爬虫抓取404的次数
192.168.1.20 - - [21/Apr/2020:14:12:49 +0800] "GET /1/index.php HTTP/1.1" 301 490 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0"192.168.1.21 - - [21/Apr/2020:15:00:49 +0800] "GET /2/index.php HTTP/1.1" 500 490 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0"
grep 'http://www.baidu.com/search/spider.html' +文件名 | grep -c 404
nginx日志分析6-统计每分钟的请求数,
并且按照请求数降序排序
192.168.1.20 - - [21/Apr/2020:14:12:49 +0800] "GET /1/index.php HTTP/1.1" 404 490 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0"192.168.1.21 - - [21/Apr/2020:15:00:49 +0800] "GET /2/index.php HTTP/1.1" 404 490 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0"
awk '{print $4}' nowcoder.txt| awk -F: '{print $2":"$3}'| awk '{a[$0]++}END{for(k in a)
print a[k],k}'| sort -k 1 -r
netstat练习1-查看各个状态的连接
数,
并且按照连接数降序输出。
Proto Recv-Q Send-Q Local Address Foreign Address Statetcp 0 0 0.0.0.0:6160 0.0.0.0:* LISTENtcp 0 0 127.0.0.53:53 0.0.0.0:* LISTENtcp 0 0 0.0.0.0:22 0.0.0.0:* LISTENtcp 0 0 172.16.56.200:41856 172.16.34.144:3306 ESTABLISHEDtcp 0 0 172.16.56.200:49822 172.16.0.24:3306 ESTABLISHEDtcp 0 0 172.16.56.200:49674 172.16.0.24:3306 ESTABLISHEDtcp 0 0 172.16.56.200:42316 172.16.34.144:3306 ESTABLISHED....
awk '{
if($NF ~ /ESTABLISHED/ || $NF ~ /LISTEN/ || $NF ~ /TIME_WAIT/)
{
a[$NF]++
}}
END{
for(i in a)print i" "a[i]
}' +文件名 | sort -nrk2
netstat练习2-查看和3306端口建立的连接
Proto Recv-Q Send-Q Local Address Foreign Address Statetcp 0 0 0.0.0.0:6160 0.0.0.0:* LISTENtcp 0 0 127.0.0.53:53 0.0.0.0:* LISTENtcp 0 0 0.0.0.0:22 0.0.0.0:* LISTENtcp 0 0 172.16.56.200:41856 172.16.34.144:3306 ESTABLISHEDtcp 0 0 172.16.56.200:49822 172.16.0.24:3306 ESTABLISHEDtcp 0 0 172.16.56.200:49674 172.16.0.24:3306 ESTABLISHED
awk '{if($5~/3306$/ && $6=="ESTABLISHED") print $5}' nowcoder.txt|
awk -F: '{a[$1]++}
END{for(k in a)
print a[k],k}'| sort -k 1 -n -r
netstat练习3-输出每个IP的连接数
awk '{
if($1~ /tcp/){
a[$5]++
}}
END{
for(i in a){
print i" "a[i]
}
}' | awk -F"[: ]" '{print $1" "$3}' | sort -nrk2
netstat练习4-输出和3306端口建立连接总的各个
Proto Recv-Q Send-Q Local Address Foreign Address Statetcp 0 0 0.0.0.0:6160 0.0.0.0:* LISTENtcp 0 0 127.0.0.53:53 0.0.0.0:* LISTENtcp 0 0 0.0.0.0:22 0.0.0.0:* LISTENtcp 0 0 172.16.56.200:41856 172.16.34.144:3306 ESTABLISHEDtcp 0 0 172.16.56.200:49822 172.16.0.24:3306 ESTABLISHED
ps分析-统计VSZ,RSS各自总和
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMANDroot 1 0.0 0.1 37344 4604 ? Ss 2020 2:13 /sbin/initroot 231 0.0 1.5 166576 62740 ? Ss 2020 15:15 /lib/systemd/systemd-journaldroot 237 0.0 0.0 0 0 ? S< 2020 2:06 [kworker/0:1H].....
awk '{
sum_vsz=sum_vsz + $5;sum_rss=sum_rss + $6
}
END{
print("MEM TOTAL \n" "VSZ_SUM:" sum_vsz/1024 "M," "RSS_SUM:" sum_rss/1024 "M")
}'
#随机获取8位字符—
md5sum
echo $RANDOM |md5sum |cut -c 1-8
#随机获取8位数字
echo $RANDOM |cksum |cut -c 1-8