How to extract and modify the last field of the uptime command output with sed or awk

Viewed 77

I need to extract the last field of the uptime command, but I also need to remove the . that is present. For example, if the output of uptime is

19:54:09 up  1:41,  1 user,  load average: 0.32, 0.37, 0.31 

I need 031. I know how to do it using pipe of awk and sed like this:

uptime | awk '{ print $NF }' | sed 's/\.//'

Is there any way to do it using only one of those two? Can they both do it without the other?

2 Answers

Just awk:

$ uptime | awk '{sub(/\./,"",$NF);print $NF}'
036

or get it from the source:

$ awk '{sub(/\./,"",$3);print $3}' /proc/loadavg 
034

Could you please try following.

uptime | awk -F'[. ]' '{print $(NF-1) $NF}'

2nd solution: With sed approach:

uptime | sed 's/.* //;s/\.//'
Related