britesc Posted May 11, 2014 Share Posted May 11, 2014 Hi, I was wondering if some kind person could help me with this. I need to make a call to file under linux that returns various SMART HDD data. From that file I want to see that each line ends in OK. I also want to extract the HDD Temperature from a line with a specific identifier on it. Filename = get_hd_smartinfo -d1 Response = 001 Raw_Read_Error_Rate 0 100 100 016 OK 002 Throughput_Performance 0 100 100 054 OK 003 Spin_Up_Time 321 (Average 164) 237 237 024 OK 004 Start_Stop_Count 6286 099 099 000 OK 005 Reallocated_Sector_Ct 0 100 100 005 OK 007 Seek_Error_Rate 0 100 100 067 OK 008 Seek_Time_Performance 0 100 100 020 OK 009 Power_On_Hours 30115 096 096 000 OK 010 Spin_Retry_Count 0 100 100 060 OK 012 Power_Cycle_Count 81 100 100 000 OK 192 Power-Off_Retract_Count 7535 094 094 000 OK 193 Load_Cycle_Count 7535 094 094 000 OK 194 Temperature_Celsius 42 142 142 000 OK 196 Reallocated_Event_Count 0 100 100 000 OK 197 Current_Pending_Sector 0 100 100 000 OK 198 Offline_Uncorrectable 0 100 100 000 OK 199 UDMA_CRC_Error_Count 0 200 200 000 OK Line = 194 Temperature_Celsius Value I want to capture = 42 in this case. I am sure there is an easier way than my tired old brain suggests, which is to output to a file, read file, grab line then somehow grab value, in a huge block of code. Thanks to anyone who can help me out. Kind regards, jB Quote Link to comment https://forums.phpfreaks.com/topic/288410-extract-value-from-a-line-in-a-file/ Share on other sites More sharing options...
Ch0cu3r Posted May 11, 2014 Share Posted May 11, 2014 Yes, regex would be quicker preg_match('~Temperature_Celsius\s+(\d+)~', $data, $m); echo 'Temperature Celsius: ' . $m[1]; Quote Link to comment https://forums.phpfreaks.com/topic/288410-extract-value-from-a-line-in-a-file/#findComment-1479073 Share on other sites More sharing options...
britesc Posted May 11, 2014 Author Share Posted May 11, 2014 Hi Ch0cu3r, Thanks for the very quick reply. Just to ensure my slow old brain has grasped this. This regex would extract the temperature ie. -23 or 0 or 42 or 123 ? Sorry to be pedantic regex is an arcane art at the moment. Kind regards, jB Quote Link to comment https://forums.phpfreaks.com/topic/288410-extract-value-from-a-line-in-a-file/#findComment-1479074 Share on other sites More sharing options...
britesc Posted May 11, 2014 Author Share Posted May 11, 2014 Hi, Tried to click the thanks button but it says I can't... Quote Link to comment https://forums.phpfreaks.com/topic/288410-extract-value-from-a-line-in-a-file/#findComment-1479075 Share on other sites More sharing options...
Ch0cu3r Posted May 11, 2014 Share Posted May 11, 2014 Yes it will return the value that follows Temperature_Celsius Although you'd need to change the \d+ regex to -?\d+ to match negative numbers preg_match('~Temperature_Celsius\s+(-?\d+)~', $data, $m); echo 'Temperature Celsius: ' . $m[1]; Quote Link to comment https://forums.phpfreaks.com/topic/288410-extract-value-from-a-line-in-a-file/#findComment-1479077 Share on other sites More sharing options...
britesc Posted May 11, 2014 Author Share Posted May 11, 2014 Once again, thank you. Just ordered an O'Reilly Book on Regex from Amazon so within the next couple of weeks I should become an expert (Yes really, the book says so). regards, jB Quote Link to comment https://forums.phpfreaks.com/topic/288410-extract-value-from-a-line-in-a-file/#findComment-1479086 Share on other sites More sharing options...
britesc Posted May 11, 2014 Author Share Posted May 11, 2014 Erm, The output is great thanks but I am having a problem iterating through the output file. For instances I was grabbing $names[12] as the line to work on, only to discover that the output varies according to the capabilities of the drive, so the line is not always $names[12]. The line from SMART that I need always starts with the value 194, so I need to be able to grab that line to work on. It could be $names[6] or $names[n]. Really appreciate the steer. Over 6 years since I coded php so tad slow at the moment, sorry. regards, jB Quote Link to comment https://forums.phpfreaks.com/topic/288410-extract-value-from-a-line-in-a-file/#findComment-1479095 Share on other sites More sharing options...
Solution Ch0cu3r Posted May 11, 2014 Solution Share Posted May 11, 2014 implode $names array into a string, then pass that to preg_match $data = implode("\n", $names); preg_match('~Temperature_Celsius\s+(-?\d+)~', $data, $m); echo 'Temperature Celsius: ' . $m[1]; Quote Link to comment https://forums.phpfreaks.com/topic/288410-extract-value-from-a-line-in-a-file/#findComment-1479098 Share on other sites More sharing options...
britesc Posted May 11, 2014 Author Share Posted May 11, 2014 Thank you very much. Kind regards, jB Quote Link to comment https://forums.phpfreaks.com/topic/288410-extract-value-from-a-line-in-a-file/#findComment-1479103 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.