Jump to content

Extract Value from a Line in a File


Go to solution Solved by Ch0cu3r,

Recommended Posts

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

 

 

Link to comment
https://forums.phpfreaks.com/topic/288410-extract-value-from-a-line-in-a-file/
Share on other sites

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  :geek:

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.