3

For example, there are some temperature data at those folders in different time. temps.txt contains the temperature number. So how can I use bash script to find out the maximum temperature? (the results only show the date,time and number of temperature,e.g. ./2011.10.20/00:00/temps.txt 27C).

$ ls
2011.10.20  2012.01.20  2012.04.16  2012.07.12  2012.10.07
2011.10.21  2012.01.21  2012.04.17  2012.07.13  2012.10.08
2011.10.22  2012.01.22  2012.04.18  2012.07.14  2012.10.09
$ cd 2011.10.20

$ ls    
00:00   02:25   04:50   07:15   09:40   12:05   14:30   16:55   19:20   21:45
00:05   02:30   04:55   07:20   09:45   12:10   14:35   17:00   19:25   21:50
00:10   02:35   05:00   07:25   09:50   12:15   14:40   17:05   19:30   21:55
$ cd 00:00
$ ls
temps.txt
$ cat temps.txt
Sensor   Location              Temp
------   --------              ----
#1        PROCESSOR_ZONE       27C/80F
  • Why don't you think of the algorithm that you might use and then try to script it in bash and tell us where you are having problems. – mdpc Nov 27 '14 at 0:47
  • You can use find to get a list of each file and then sort on each file to have it sorted by temperature; after that it's a matter of formatting the output to your taste. – outlyer Nov 27 '14 at 1:23
4

You can use the combination find, grep and awk command to get the desired result. The below is a oneliner which will print the file which has the maximum temperature recorded.

find . -mindepth 3 -exec echo -n "{} " \; -exec grep "PROCESSOR_ZONE" {} \; |
awk '{
    split($4,val,"/");
    gsub("C","",val[1]);
    if (max<val[1]) {file=$1; max=val[1]}
} END {print(file)}'

Output

./2012.04.16/00:10/temps.txt

Below is the script version of the oneliner.

#!/bin/bash

# The path where temperature directories and files are kept
path="/tmp/tmp.ADntEuTlUT/"

# Temp file
tempfile=$(mktemp)

# Get the list of files name and their corresponding
# temperature data.
find "${path}" -mindepth 3 -exec echo -n "{} " \; -exec grep "PROCESSOR_ZONE" {} \; > "${tempfile}"

# Parse though the temp file to find the maximum 
# temperature based on Celsius
awk '{split($4,val,"/");gsub("C","",val[1]);if(max<val[1]){file=$1;max=val[1]}} END{print(file)}' "${tempfile}"

# Removing the temp file
rm -f "${tempfile}"
0

With GNU grep, assuming no file paths contain newline characters:

grep -rHPo 'PROCESSOR_ZONE\s+\K\d+C' . | awk -F: '
   0+$NF >= max {max = $NF; text = $0}; END {print text}'
-1

This solution makes used of the split function in awk to split the fields and do the numeric reverse sort to pop the max number to the top.

find . -name "temps.txt" -print|xargs tail -n 1 | awk '{split($NF,temp,"[CF/]");print temp[1]}'|sort -r | head -n 1
  • Although this code may help to solve the problem, providing additional context regarding why and/or how it answers the question would significantly improve its long-term value. Please edit your answer to add explanation, including what limitations and assumptions apply. – Toby Speight Aug 3 '16 at 14:12
  • That doesn't show the file name. Note that your need sort -rn for a numerical sort. – Stéphane Chazelas Aug 3 '16 at 15:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.