Grep

如何合併這 2 個跟踪腳本的輸出

  • September 4, 2020

我有 2 個腳本:

track_temps.sh

while true
do
   echo -n "$(date +"%H:%M:%S"): "
   sensors | grep Tdie

   # Sleeping for X seconds
   sleep 1
done

track_mhz.sh

   while true
do
   # Printing the time and all temperatures to stdout
   echo -n "$(date +"%H:%M:%S"): "
   cat /proc/cpuinfo | grep "MH" | sort -r | head -1

   # Sleeping for X seconds
   sleep 1
done

track_temps.sh 的輸出如下所示:

09:31:44: Tdie:         +69.1°C  (high = +70.0°C)
09:31:45: Tdie:         +69.1°C  (high = +70.0°C)
09:31:46: Tdie:         +69.1°C  (high = +70.0°C)

track_mhz.sh 的輸出如下所示:

09:32:01: cpu MHz       : 4015.803
09:32:02: cpu MHz       : 4008.034
09:32:03: cpu MHz       : 4028.516

我想合併輸出,使它看起來像

09:31:44: Tdie:         +69.1°C  (high = +70.0°C) cpu MHz: 4015.803
09:31:45: Tdie:         +69.1°C  (high = +70.0°C) cpu MHz: 4008.034
09:31:46: Tdie:         +69.1°C  (high = +70.0°C) cpu MHz: 4028.516

更好的是

我會嘗試

while true
do
   echo -n "$(date +"%H:%M:%S"): "
   echo -n "$(sensors | grep Tdie) "
   awk '$2 == "MHz" { if (c< $4) c=$4;} END {printf "cpu MHz %s\n",c}'  /proc/cpuinfo

   sleep 1
done 
  • 在我的盒子 cpu 頻率從 998 變為 1200 MHz 時,我不確定是否sort -r會按預期執行。(字元串“900”大於字元串“1200”)

引用自:https://unix.stackexchange.com/questions/607800