bash:parse_a_line_from_a_file
This is an old revision of the document!
Bash - Parse a line from a file
Example 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)
#!/bin/bash fil=/home/test.log # Test for existence of the test 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
Returns:
Current temperature is: 78 Current temperature is: 78
Only get last temperature record
#!/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
bash/parse_a_line_from_a_file.1467421162.txt.gz · Last modified: 2020/07/15 09:30 (external edit)