NetBSD-Users archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: Dunce awk question



On 09/26/15 13:41, Dan LaBell wrote:

On Sep 25, 2015, at 6:41 AM, William A. Mahaffey III wrote:



I am trying to use awk & grep to fashion a command to print out HDD temps, along w/ some identifying info:

I've been doing something similar, except with sed
#!/bin/mksh -p
mbmon -c 1 | sed -n '2{
h
s/, .*$//
p
}
'

atactl wd0d smart status | sed -n '/^194/{
h
s/^194.*Temperature[ ]*/wd0d =/
p
}
#tempget
Temp.= 31.0
wd0d =  28
The first grabs only line 2, and deletes everything after the first comma.
The second grabs the line the begins with 194...

If I wanted do that with awk.
#atactl wd0d smart status| awk '/^194/{print ($8) } '
33
or
#atactl wd0d smart status| awk '/^194/{print ($7, "wd0d", $8) } '
Temperature wd0d 34
I can grab the model like this.
#atactl wd0d identify | awk '/^Model:/{print ($2) }'
ST380021A,
Which leaves me with a pesky comma, or with sed.
#atactl wd0d identify | sed -n '1{
h
s/^Model://
s/, .*$//
p
}'
 ST380021A


I am only *weakly* familiar w/ GNU awk, where the above works.


awk's printf would expect to begin with format like C's printf. The standard idiom is to use '{print $8}', but then if you want to do more, you have to remember
both the parens, and the commas, so now I always do '{print ($8)}' .


I wound up fancying everything up into a shell script:


[wam@4256EE1, ~, 7:31:08am] 523 % cat `which hddtemp`
#! /bin/csh -f

# Using your version string above, the following works for me:
#
# % echo 'Model: HGST HTS721010A9E630, Rev: JB0OA3J0, Serial #:
# JR10046P1D5UXN' | awk '/Model/ { print $3 }'
# HTS721010A9E630,
#
# Some rules I usually go by:
# 1. Piping through grep and awk is almost always wrong - awk has some
# fine regexps, you should just use them
# 2. Different disks and virtual disks report using different formats.
# For instance, for me on a VM:
#
# % sudo atactl wd0 identify
# Model: VMware Virtual IDE Hard Drive, Rev: 00000001, Serial #:
# 00000000000000000001
#
# and the third field in that string is "Virtual"
#
# 3. Most uses of "just print this" in awk for me use the "print" verb.
# "printf" needs a format string, same as in C.


# echo 'Model: HGST HTS721010A9E630, Rev: JB0OA3J0, Serial #: JR10046P1D5UXN' | awk '/Model/{print $3}'

echo "SMART supported, SMART enabled"

foreach drive ($*)
echo -n 'drive '$drive': '; sudo atactl $drive identify | awk '/Model/ {ORS = ", "; print $2, $3, "S/N:", $8}' - sudo atactl $drive smart status | awk '/Temp/ {print "Temp.", $8, "degC,", $9, $10, $11, "degC"}' - # echo -n 'drive '$drive': '; sudo atactl $drive identify | grep Model | awk '{ORS = ", "; print $2, $3, "S/N:", $8}' - # sudo atactl $drive smart status | grep Temp | awk '{print "Temp.", $8, "degC", $9, $10, $11, "degC"}' -
end
[wam@4256EE1, ~, 7:41:46pm] 524 % envstat ; hddtemp wd0 wd1 wd2 wd3 wd4 wd5
                      Current  CritMax  WarnMax  WarnMin  CritMin Unit
[amdtemp0]
  cpu0 temperature:    33.000 degC
SMART supported, SMART enabled
drive wd0: HGST HTS721010A9E630, S/N: JR10046P1D5UXN, Temp. 31 degC, Lifetime min/max 21/40 degC drive wd1: HGST HTS721010A9E630, S/N: JR10046P1D605N, Temp. 32 degC, Lifetime min/max 21/42 degC drive wd2: HGST HTS721010A9E630, S/N: JR10046P1D5TZN, Temp. 31 degC, Lifetime min/max 21/40 degC drive wd3: HGST HTS721010A9E630, S/N: JR10046P1EK9GN, Temp. 32 degC, Lifetime min/max 21/41 degC drive wd4: HGST HTS721010A9E630, S/N: JR10046P1EJ4UN, Temp. 30 degC, Lifetime min/max 20/38 degC drive wd5: HGST HTS721010A9E630, S/N: JR10046P1D5V2N, Temp. 31 degC, Lifetime min/max 21/39 degC
[wam@4256EE1, ~, 7:43:24pm] 525 %


*Wooooohooo* !!!! Thanks for all the pointers :-).


--

	William A. Mahaffey III

 ----------------------------------------------------------------------

	"The M1 Garand is without doubt the finest implement of war
	 ever devised by man."
                           -- Gen. George S. Patton Jr.



Home | Main Index | Thread Index | Old Index