比较文件夹中的所有文件

时间:2020-11-26 22:50:22

I've got script in crontab which creates every 30 minutes files with list of Offline peers in asterisk:

我在crontab中有脚本,每隔30分钟创建一个带有星号中脱机对等列表的文件:

now=$(date +"%Y%m%d%H%M")
/usr/sbin/asterisk -rx 'sip show peers' | grep "Unspec" | sed 's/[/].*//' >> /var/log/asterisk/offline/offline_$now

I need to parse theese files and find extensions that were always offline, i.e. stings in files that were constant.

我需要解析theese文件并找到始终处于脱机状态的扩展名,即在不变的文件中查找。

How can I do this?

我怎样才能做到这一点?

Output is:

/usr/sbin/asterisk -rx 'sip show peers' | grep "Unspec" | sed 's/[/].*//' | tail -3
891
894
899

ls /var/log/asterisk/offline/
offline_201309051400  offline_201309051418  offline_201309051530  offline_201309051700 
offline_201309051830  offline_201309052000  offline_201309052130
offline_201309051405  offline_201309051430  offline_201309051600  offline_201309051730  
offline_201309051900  offline_201309052030  offline_201309052200
offline_201309051406  offline_201309051500  offline_201309051630  offline_201309051800    
offline_201309051930  offline_201309052100  offline_201309052230

1 个解决方案

#1


0  

This awk script will print the lines that are present in all of the files:

这个awk脚本将打印所有文件中的行:

awk 'FNR==1{f++}{a[$0]++}END{for (i in a) if (a[i]==f) print i}' offline_*

How it works:

这个怎么运作:

  • With FNR==1{f++} we count the number of files that are parsed (FNR is equal to one for the first line of each file)
  • 使用FNR == 1 {f ++},我们计算解析的文件数(FNR等于每个文件的第一行一个)

  • with {a[$0]++} we count how many times each line has appeared.
  • 用{a [$ 0] ++}我们计算每行出现的次数。

  • the END block prints the elements of the array that have been found f times.
  • END块打印已找到f次的数组元素。

#1


0  

This awk script will print the lines that are present in all of the files:

这个awk脚本将打印所有文件中的行:

awk 'FNR==1{f++}{a[$0]++}END{for (i in a) if (a[i]==f) print i}' offline_*

How it works:

这个怎么运作:

  • With FNR==1{f++} we count the number of files that are parsed (FNR is equal to one for the first line of each file)
  • 使用FNR == 1 {f ++},我们计算解析的文件数(FNR等于每个文件的第一行一个)

  • with {a[$0]++} we count how many times each line has appeared.
  • 用{a [$ 0] ++}我们计算每行出现的次数。

  • the END block prints the elements of the array that have been found f times.
  • END块打印已找到f次的数组元素。