【shell】两个文件对应列相减|文件合并|网卡流量清0方法
目录
网卡流量清0方法
网上很多方法都是重载驱动。觉得不好。我的方法是 启动脚本记录当前数值,然后每隔一定时间(默认1s)读取新值减去初始值得到脚本启动到当前的统计。相当于每次启动就“清0”了一次。
脚本:
stat.sh
用法:
stat.sh eth0 #默认1秒刷新
或
stat.sh eth0 2 #2秒刷新
stat.sh
#!/bin/bash
set -o errexit
BEFORE=/tmp/beforeStat.txt
AFTER=/tmp/afterStat.txt
JOIN_FILE=/tmp/Joinfile.txt
INTERFACE=$1
if [ -z $1 ]; then
echo -e "\033[31mError:\033[0m"
echo -e "\033[31mPlease Input interface name, example: eth0 \033[0m"
exit 0
fi
if [ -z $2 ]; then
INTERVAL=1
else
INTERVAL=$2
fi
ifconfig ${INTERFACE} | grep -E "RX|TX"|awk '{print $1,$2,$3,$4,$5}' > ${BEFORE}
function loop()
{
ifconfig ${INTERFACE} | grep -E "RX|TX"|awk '{print $3,$4,$5}' >${AFTER}
paste ${BEFORE} ${AFTER} > ${JOIN_FILE}
cat ${JOIN_FILE} |column -t|awk '{print $1,$2,$6-$3,$4,($8-$5),"="($8-$5)/(1024*1024)"MB"}'|column -t
}
echo "Port: "$INTERFACE "interval: "$INTERVAL" s"
while((1)); do
sleep ${INTERVAL}
clear
loop
done
两个文件对应列相减
有两个文件,格式如下
app1,20,30,2,55
app2,23,44,56,58
app3,32,32,6,59
app1,25,37,24,59
app2,27,64,58,66
app3,39,72,9,70
对应位置相减后输出:
app1,5,7,22,4
app2,4,20,2,8
app3,7,40,3,11
用shell如何实现?
===================
$ join file1 file2 -t, -j1 | awk -F, 'BEGIN{OFS = ","}{ print $1, $6-$2, $7-$3, $8-$4, $9-$5 }'
===================
$ cat file1
app1,20,30,2,55
app2,23,44,56,58
app3,32,32,6,59
$ cat file2
app1,25,37,24,59
app2,27,64,58,66
app3,39,72,9,70
$ awk -F, -vOFS=',' 'NR==FNR{for(i=1;i++ app1,5,7,22,4
app2,4,20,2,8
app3,7,40,3,11
两个文件合并的方法
一个文件在上,一个文件在下
cat file1 file2 > file3
一个文件在左,一个文件在右
paste file1 file2 > file3
统计prio
ethtool -S ib3b-0 | grep prio > a.txt
ethtool -S ib3b-0 | grep prio > b.txt
paste a.txt b.txt > c.txt
cat c.txt |column -t|awk '{print $1 $3-$2}'
#!/bin/bash
set -o errexit
BEFORE=/tmp/beforeStat.txt
AFTER=/tmp/afterStat.txt
JOIN_FILE=/tmp/Joinfile.txt
FLITER="prio0|prio1|prio2"
INTERFACE=$1
if [ -z $1 ]; then
echo -e "\033[31mError:\033[0m"
echo -e "\033[31mPlease Input interface name, example: eth0 \033[0m"
exit 0
fi
if [ -z $2 ]; then
INTERVAL=1
else
INTERVAL=$2
fi
ethtool -S ${INTERFACE} | grep prio > ${BEFORE}
function loop()
{
ethtool -S ${INTERFACE} | grep prio |awk '{print $2}' >${AFTER}
paste ${BEFORE} ${AFTER} > ${JOIN_FILE}
cat ${JOIN_FILE} |column -t|awk '{print $1 $3-$2}'|grep -E $FLITER
}
echo "Port: "$INTERFACE "interval: "$INTERVAL" s"
while((1)); do
sleep ${INTERVAL}
clear
loop
done