You are not logged in.

#1 2009-04-09 07:19:08

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,677
Website

usage of sed question for experts

I need a little help with sed.  Basically, I need to parse out selections from the output of hddtemp so conky can display some hdd temps for me.  I have hddtemp in daemon mode so A simple 'nc localhost 7634' displays the following:

$ nc localhost 7634
|/dev/sg1|ST3640323AS|31|C||/dev/sg2|ST3640323AS|31|C||/dev/sg3|WDC WD1001FALS-00J7B1|36|C||/dev/sg4|WDC WD7501AALS-00J7B0|32|C||/dev/sda|ST3640323AS|31|C||/dev/sdb|ST3640323AS|31|C||/dev/sdc|WDC WD1001FALS-00J7B1|36|C||/dev/sdd|WDC WD7501AALS-00J7B0|32|C|

Just ignore /dev/sdd because it's going away soon. How can I use sed to extract the temps of /dev/sda and /dev/sdb and /dev/sdc so I can insert an appropriate line to my .conkyrc?

I have can do this long hand using the '-cut -cx-y' command:

sda:$color ${execi 300 nc localhost 7634 | cut -c50-51} C

...but I think a series of sed statements would be more power (one to delete everything up to the temp and one to delete everything after the temp) particularlly if the HDD gets swapped out which would change the number of characters in the line.

Thoughts are welcomed and thanks!


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#2 2009-04-09 08:35:16

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: usage of sed question for experts

Like this?

echo '|/dev/sg1|ST3640323AS|31|C||/dev/sg2|ST3640323AS|31|C||/dev/sg3|WDC WD1001FALS-00J7B1|36|C||/dev/sg4|WDC WD7501AALS-00J7B0|32|C||/dev/sda|ST3640323AS|31|C||/dev/sdb|ST3640323AS|31|C||/dev/sdc|WDC WD1001FALS-00J7B1|36|C||/dev/sdd|WDC WD7501AALS-00J7B0|32|C|' | sed 's/.*sda|[^|]*|\([0-9]*\)|C|.*sdb|[^|]*|\([0-9]*\)|C|.*sdc|[^|]*|\([0-9]*\)|C|.*/sda:$color \1 C\nsdb:$color \2 C\nsdc:$color \3 C/'

Different approach:

echo '|/dev/sg1|ST3640323AS|31|C||/dev/sg2|ST3640323AS|31|C||/dev/sg3|WDC WD1001FALS-00J7B1|36|C||/dev/sg4|WDC WD7501AALS-00J7B0|32|C||/dev/sda|ST3640323AS|31|C||/dev/sdb|ST3640323AS|31|C||/dev/sdc|WDC WD1001FALS-00J7B1|36|C||/dev/sdd|WDC WD7501AALS-00J7B0|32|C|' | sed 's/^|//;s/|$//;s/||/\n/g' | sed -n '/sd[abc]/{s/.dev.//;s/|[^|]*|/:$color /;s/|/ /p}'

Also fun:

paste -d' ' <(echo sda:\$color;echo sdb:\$color;echo sdc:\$color) <(echo '|/dev/sg1|ST3640323AS|31|C||/dev/sg2|ST3640323AS|31|C||/dev/sg3|WDC WD1001FALS-00J7B1|36|C||/dev/sg4|WDC WD7501AALS-00J7B0|32|C||/dev/sda|ST3640323AS|31|C||/dev/sdb|ST3640323AS|31|C||/dev/sdc|WDC WD1001FALS-00J7B1|36|C||/dev/sdd|WDC WD7501AALS-00J7B0|32|C|' | tr '|' '\n' | grep -EA2 '(sda|sdb|sdc)' | grep '^[0-9]') <(echo C; echo C; echo C)

EDIT:
And let's not forget awk.

echo '|/dev/sg1|ST3640323AS|31|C||/dev/sg2|ST3640323AS|31|C||/dev/sg3|WDC WD1001FALS-00J7B1|36|C||/dev/sg4|WDC WD7501AALS-00J7B0|32|C||/dev/sda|ST3640323AS|31|C||/dev/sdb|ST3640323AS|31|C||/dev/sdc|WDC WD1001FALS-00J7B1|36|C||/dev/sdd|WDC WD7501AALS-00J7B0|32|C|' | awk 'BEGIN{RS="\\|\\|";FS="|"} /sd[abc]/{print substr($1,6) ": $color " $3 " C"}'

Last edited by Procyon (2009-04-09 08:51:31)

Offline

#3 2009-04-09 17:04:57

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,677
Website

Re: usage of sed question for experts

@Procyon - thanks for the reply... your usage of sed is amazing.  I can't seem to cut back what you gave me here to insert into my .conkyrc though. What I'd like to do is simply run the command three times (once, for sda, once for sdb, and once for sdc).  The think is, the only output I need to see is the temp, just two numerical digits.  Can you cut the following line you proposed down to accomplish it?

echo '|/dev/sg1|ST3640323AS|31|C||/dev/sg2|ST3640323AS|31|C||/dev/sg3|WDC WD1001FALS-00J7B1|36|C||/dev/sg4|WDC WD7501AALS-00J7B0|32|C||/dev/sda|ST3640323AS|31|C||/dev/sdb|ST3640323AS|31|C||/dev/sdc|WDC WD1001FALS-00J7B1|36|C||/dev/sdd|WDC WD7501AALS-00J7B0|32|C|' | sed 's/^|//;s/|$//;s/||/\n/g' | sed -n '/sda/{s/.dev.//;s/|[^|]*|/:$color /;s/|/ /p}'

CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#4 2009-04-09 17:22:49

tam1138
Member
Registered: 2007-09-10
Posts: 238

Re: usage of sed question for experts

Why do you run hddtemp in daemon mode?  Why not just invoke hddtemp [drive] directly from conky?  The only benefit I see is saving yourself a fork, and that seems like an unnecessary optimization to me, especially given how much cleaner it will make your queries.

Offline

#5 2009-04-09 17:23:14

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: usage of sed question for experts

Thanks. I see the difference now, so $color wasn't interpreted?

So in conkyrc syntax it would become:

sda:$color {execi 300 nc localhost 7634 | sed 's/^|//;s/|$//;s/||/\n/g' | sed -n '/sda/{s/.dev.sd.//;s/|[^|]*|//;s/|.*//p}'} C

Offline

#6 2009-04-09 18:33:50

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,677
Website

Re: usage of sed question for experts

@Procyon - thank you for the code.  I added a dollar sign to before the brackets and it works great. 

Screenshot and .conkyrc for anyone interested:
conky.gif

# for this to work you need both lm-sensors and hddtemp
# get both from main repos

# set to yes if you want Conky to be forked in the background
background no

#cpu_avg_samples 2
#net_avg_samples 2

own_window yes
own_window_type override
own_window_transparent yes
own_window_hints undecorated,below,sticky,skip_taskbar,skip_pager

out_to_console no

# X font when Xft is disabled, you can pick one with program xfontsel
#font 7x12
#font 6x10
#font 7x13
#font 8x13
#font 7x12
#font *mintsmild.se*
#font -*-*-*-*-*-*-34-*-*-*-*-*-*-*
#font -artwiz-snap-normal-r-normal-*-*-100-*-*-p-*-iso8859-1

# Use Xft?
use_xft yes

# Xft font when Xft is enabled
xftfont Bitstream Vera Sans Mono:size=8

#own_window_transparent no
#own_window_colour hotpink
# Text alpha when using Xft
xftalpha 0.8

on_bottom yes

# Update interval in seconds
update_interval 2
# Create own window instead of using desktop (required in nautilus)
#own_window no

# Use double buffering (reduces flicker, may not work for everyone)
double_buffer yes

# Minimum size of text area
#minimum_size 250 5
maximum_width 258

# Draw shades?
draw_shades no

# Draw outlines?
draw_outline no

# Draw borders around text
draw_borders no

# Stippled borders?
stippled_borders 10

# border margins
border_margin 4

# border width
border_width 1

# Default colors and also border colors
default_color white
default_shade_color white
default_outline_color white

# Text alignment, other possible values are commented
#alignment top_left
#minimum_size 10 10
#alignment top_right
alignment bottom_left
#alignment bottom_right

# Gap between borders of screen and text
gap_x 12
gap_y 37
# Add spaces to keep things from moving about?  This only affects certain objects.
use_spacer no

# Subtract file system buffers from used memory?
no_buffers yes

# set to yes if you want all text to be in uppercase
uppercase no

TEXT
${color #ddaa00}$nodename$color
$sysname $kernel on $machine
${color lightgrey}Uptime:$color $uptime ${color lightgrey}- Load:$color $loadavg${color lightgrey}
RAM Usage:$color $memperc% or $mem of $memmax${color lightgrey}
Swap Usage:$color $swapperc%${color lightgrey}
$color$stippled_hr${color lightgrey}
Intel Xeon X3360 @ $color${freq_g} GHz${color lightgrey}
Average CPU History:
${color black}${cpugraph 000000 5000a0}${color lightgrey}
 ${color lightgrey}Core0:$color ${execi 8 sensors | grep -A 1 'Core 0' | cut -c15-16 | sed '/^$/d'} C${color grey} @$color ${cpu cpu1}%   ${alignr}${cpubar cpu1 6,120}
 ${color lightgrey}Core1:$color ${execi 8 sensors | grep -A 1 'Core 1' | cut -c15-16 | sed '/^$/d'} C${color grey} @$color ${cpu cpu2}%   ${alignr}${cpubar cpu2 6,120}
 ${color lightgrey}Core2:$color ${execi 8 sensors | grep -A 1 'Core 2' | cut -c15-16 | sed '/^$/d'} C${color grey} @$color ${cpu cpu3}%   ${alignr}${cpubar cpu3 6,120}
 ${color lightgrey}Core3:$color ${execi 8 sensors | grep -A 1 'Core 3' | cut -c15-16 | sed '/^$/d'} C${color grey} @$color ${cpu cpu4}%   ${alignr}${cpubar cpu4 6,120}${color grey}
$color$stippled_hr${color lightgrey}
Thermal Monitors:${color lightgrey}
${color grey} GPU:$color ${execi 8 nvclock -T | grep 'GPU t' | cut -c21-22} C${color grey}      MB:$color ${execi 8 sensors | grep temp3 | cut -c15-16} C
${color grey} sda:$color ${execi 300 nc localhost 7634 | sed 's/^|//;s/|$//;s/||/\n/g' | sed -n '/sda/{s/.dev.sd.//;s/|[^|]*|//;s/|.*//p}'} C     ${color grey}sdb:$color ${execi 300 nc localhost 7634 | sed 's/^|//;s/|$//;s/||/\n/g' | sed -n '/sdb/{s/.dev.sd.//;s/|[^|]*|//;s/|.*//p}'} C${color grey}     sdc:$color ${execi 300 nc localhost 7634 | sed 's/^|//;s/|$//;s/||/\n/g' | sed -n '/sdc/{s/.dev.sd.//;s/|[^|]*|//;s/|.*//p}'} C${color grey}
$stippled_hr${color light grey}
${color lightgrey}Processes:$color $processes  ${color grey}Running:$color $running_processes
${color}Name              PID     CPU%   MEM%
${color #ddaa00} ${top name 1} ${top pid 1} ${top cpu 1} ${top mem 1}
${color lightgrey} ${top name 2} ${top pid 2} ${top cpu 2} ${top mem 2}
${color lightgrey} ${top name 3} ${top pid 3} ${top cpu 3} ${top mem 3}
${color}Mem usage
${color #ddaa00} ${top_mem name 1} ${top_mem pid 1} ${top_mem cpu 1} ${top_mem mem 1}
${color lightgrey} ${top_mem name 2} ${top_mem pid 2} ${top_mem cpu 2} ${top_mem mem 2}
${color lightgrey} ${top_mem name 3} ${top_mem pid 3} ${top_mem cpu 3} ${top_mem mem 3}

Last edited by graysky (2009-04-09 18:34:48)


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

Board footer

Powered by FluxBB