WiFi网络作为集群条形图

时间:2018-06-04 15:22:06

标签: bash shell gnuplot

我正在使用我的Airport实用程序来获取我周围所有可用的WiFi网络,我想使用gnuplot将它们显示为聚类条形图。我无法弄清楚,如何将数据转换为gnuplot可以使用的形式。

我的neighbours.dat看起来像这样:

CHANNEL RSSI SSID
1  -54  DESIGN-DEV
1  -54  DESIGN-EMPLOYEE-DA
1  -54  DESIGN-GUEST
1  -56  DESIGN-DEV
1  -56  DESIGN-EMPLOYEE-DA
1  -56  DESIGN-GUEST
1  -56  cwlanoffice
1  -56  llc-net
1  -66  DESIGN-DEV
1  -66  DESIGN-EMPLOYEE-DA
1  -66  DESIGN-GUEST
6  -56  WLAN-A23120
6  -60  WLAN-73CF94
6  -71  DESIGN-EMPLOYEE-DA
6  -71  DESIGN-GUEST
6  -72  DESIGN-DEV
6  -74  ecp-eco2016
6  -75  cwlanoffice
6  -75  llc-net
6  -76  CWLANGuest
6  -84  CWLANGuest
6  -84  TIMTIM
6  -85  llc-net
11  -39  WLAN-BKH64S
11  -67  cwlanoffice
11  -67  llc-net
11  -69  CWLANGuest
36  -57  WLAN-73CF94
36  -89  CWLANGuest
36  -89  cwlanoffice
36  -90  llc-net
44  -65  CWLANGuest
44  -66  cwlanoffice
44  -66  llc-net
44  -80  ecp-eco2016
52  -40  WLAN-BKH64S
52  -56  DESIGN-DEV
52  -56  DESIGN-GUEST
52  -57  DESIGN-EMPLOYEE-DA
100  -73  DESIGN-DEV
100  -73  DESIGN-GUEST
100  -74  DESIGN-EMPLOYEE-DA
108  -47  DESIGN-DEV
108  -47  DESIGN-EMPLOYEE-DA
108  -47  DESIGN-GUEST
108  -61  DESIGN-DEV
108  -61  DESIGN-EMPLOYEE-DA
108  -62  DESIGN-GUEST
132  -66  CWLANGuest
132  -66  cwlanoffice
132  -66  llc-net

对于bar chart,我需要将其置于此格式中 - 将SSID作为我的列,将CHANNEL作为我的行:

         SSID1   SSID2   SSID3   SSID4   SSID5
Channel1 -56     -20     -19             -47
Channel2         -38     -21             -28
你可以帮助我实现这个目标吗?我最近的尝试是使用唯一的SSID并尝试使用它们创建一个表。

sort -k3 -u neighbours.dat | awk '{print $3}' > neighbours-ssids.dat

c=$(wc -l < neighbours-ssids.dat)

echo $c

for (( i = 0; i < $c; i++ )); do
  awk 'NR=='$i neighbours-ssids.dat
done

1 个答案:

答案 0 :(得分:0)

A gawk solution could be as follows:

BEGIN {
  num_of_ssids = 0
  num_of_channels = 0
}
{
  ssid = $3
  if (!(ssid in data_ssid)) {
    data_ssid[ssid] = num_of_ssids;
    idx_to_ssid[num_of_ssids] = ssid;
    num_of_ssids++;
  }

  channel = $1
  if (!(channel in data_chanel)) {
    data_chanel[channel] = num_of_channels;
    idx_to_channel[num_of_channels] = channel;
    num_of_channels++;
  }

  data[channel][ssid] = $2
}

END{
  for(j=0;j<num_of_ssids;j++) {
    ssid = idx_to_ssid[j];
    printf "\t%s", ssid;
  }
  printf "\n";

  for(i=0;i<num_of_channels;i++) {
    channel = idx_to_channel[i];
    printf "%s", channel;

    for(j=0;j<num_of_ssids;j++) {
      ssid = idx_to_ssid[j];
      printf "\t%s", data[channel][ssid]; 
    }
    printf "\n";
  }
}

The idea is to go over the file and for each line:

  1. check if the ssid has been already seen or not (similarly for the channel) and if not then assign to it an incremental index (in order to associate this index with row/column index of the resulting table)
  2. save the corresponding data value into a two-dimensional array indexed by channel and ssid name

At the end of the script, one can iterate over all unique channel/ssid names and print the corresponding value in a form of a table.