I’m trying to get specific data from the smartctl command. I want to get the total power_on_hours, but I don’t want all the filler. I just want the straight hours. This is the command I’ve come up with now:
smartctl -A /dev/sda | grep Power_On_Hours
My output is then something similar to:
9 Power_On_Hours 0x0032 100 100 000 Old_age Always - 27
I want the last number on the line. I figure that I can probably somehow read to the end of the file and count back to the first space or search until the hyphen and read until the end of the file. I just don’t know how I would do that. My experience with grep is way too low to know how to approach this. My goal is to have the output be something like:
27
Any help would be much appreciated.
If you can/want to use
awkit is easy to grab the last field withawk '{print $NF}'. E.g., building on what you already have:NFis the awk built-in variable for number of fields. Using the$NFmeans to use the contents of that field.However, you could shorten this (and eliminate
grep) by just havingawksearch for the string: