Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How to find out the biggest number in many documents that contains different numbers without wasting too much if your time.
The question is published on by Tutorial Guruji team.
The question is published on by Tutorial Guruji team.
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
Answer
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}"
We are here to answer your question about How to find out the biggest number in many documents that contains different numbers - If you find the proper solution, please don't forgot to share this with your team members.