Hello Linux fans. I am kinda new to Linux and Bash scripting and would like to read through a log file looking for a word, then extract a value later on that line.
Here is a sample of my log file
07/17 21:04:01 sndc addr unit 1 : hu P1 (TempLinc)
07/17 21:04:02 sndc func StatusReq : hc P
07/17 21:04:04 rcvi addr unit 15 : hu P15 (TempAck_5)
07/17 21:04:04 rcvi func Preset : level 11
07/17 21:04:04 Temperature = 78 : hu P0 (office_temp)
07/17 21:19:01 sndc addr unit 1 : hu P1 (TempLinc)
07/17 21:19:02 sndc func StatusReq : hc P
07/17 21:19:05 rcvi addr unit 15 : hu P15 (TempAck_5)
07/17 21:19:05 rcvi func Preset : level 11
07/17 21:19:05 Temperature = 78 : hu P0 (office_temp)
I have the beginnings of a script that can read the file line by line, thus:
#!/bin/bash
fil=/home/heyu.log.ttyS0
#test for existence of the heyu log file
if [ -f $fil ]
then
#read through the file looking for the word Temperature =
while read line
do
echo $line
Ok, do something here
done < $fil
fi
I want to search the line for the word "Temperature = ", then get the value in a variable.
I’m sure that there are many ways how to do this trick with bash. I’m not sure what exactly you need but have a look at the example below. I hope that you can modify it to fit your purpose:
#!/bin/bash
fil=/home/heyu.log.ttyS0
#test for existence of the heyu log file
if [ -f $fil ]
then
#read through the file looking for the word Temperature =
while read line
do
echo $line | grep -q Temperature
if [ $? == 0 ]; then
mytemp=`echo $line | cut -d = -f2 | cut -d : -f1`
echo "Current temperature is: $mytemp"
fi
done < $fil
fi
This will output:
Current temperature is: 78
Current temperature is: 78
I have used mytemp variable to hold temperature value. Hope this helps.
Wonderful. That works as advertised. Thank you. I knew it might be that easy.
What are the costs of invoking grep for each time through the loop? I only need the last temperature (it logs the temp every 15 minutes and I only need the most current.)
Might there be a shortcut method?
Is it possible for me to start at the end of the file and skip backwards and use the first one I come to to reduce the number of calls to grep?
It might not be that bad, it logs them 4 time an hour times 24 hours is a maximum of 96 (plus overhead of the other stuff.) means it is a pretty short file. It gets log rotated each day. Maybe I will stop while I am ahead.
Yes there is a shortcut. In my previous code I was simply following your example without knowing all requirements. If the last temperature is all what we need then our bash code can be rewritten to something like this:
#!/bin/bash
fil=/home/heyu.log.ttyS0
if [ -f $fil ]
then
mytemp=`grep Temperature $fil | tail -1 | cut -d = -f2 | cut -d : -f1`
echo "Current temperature is: $mytemp"
fi
This will output:
Current temperature is: 78
I’m sure that even this code can be shortened even further. I hope this helps. Thanks for using our forums.