usage of sed question for experts


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting usage of sed question for experts
# 1  
Old 04-09-2009
PHP 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:

Code:
$ 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:
Code:
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!
# 2  
Old 04-09-2009
Try this to retrieve /dev/sda. It might look a little complicated.
Code:
echo $(nc localhost 7634) | sed -n -e "s+.*\(|/dev/sda|[^|]*|[^|]*|[^|]*|\).*+\1+p"

But I think awk would be a good tool to retrieve the columns you are looking for.
# 3  
Old 04-09-2009
Parsing hddtemp input

I agree that awk is the tool for this.
It will handle changes in the length of disk names, etc.
better than some other approaches.

Assuming that more than one disk temp is on each input line,
this does something like what you seem to want.
It assumes that the right number of fields will always be provided after the disk name.

Code:
devs="/dev/sda /dev/sdb /dev/sdc"

nc localhost 7634  | nawk -F '|' -v devs="$devs"  '
NR ==1 { ndev = split(devs,dev," ") ; if (ndev < 1) exit(1) }

{
        for (i = 1 ; i <= NF; i++) {
                for (d = 1; d <= ndev; d++) {
                        if ($i == dev[d])
                                print dev[d],$(i+2);
                }
        }
}'  -

/dev/sda 31
/dev/sdb 31
/dev/sdc 36
# 4  
Old 04-09-2009
PHP

Thanks guys...

Last edited by audiophile; 04-09-2009 at 05:42 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Sed command usage question

How to work x in sed command? I know x command is swaps the contents of pattern space and hold space. But i am unable to understand it's working? (4 Replies)
Discussion started by: Vartika18
4 Replies

2. Shell Programming and Scripting

Couple of easy questions for experts on awk/sed

Hello Experts.. I have 3-4 C codes with Oracle SQL statements embedded. All the SQL statements starts with EXEC SQL keyword and ends with ;. I want to extract all the SQL statements out of these codes. I did awk '/^EXEC SQL/,/\;/' inputFile (I use this on all of the codes individually). That... (2 Replies)
Discussion started by: juzz4fun
2 Replies

3. UNIX for Dummies Questions & Answers

Ln usage question

Is it possible to assign a symbolic link to a network folder, without that folder being mounted? Thanks! (1 Reply)
Discussion started by: nerdcurious
1 Replies

4. Shell Programming and Scripting

awk question for experts

Hi guys I am trying to perform a substitution using 'awk' command, but it fails. I work in ksh. Here is my code: $ line="F 18:30 10 23:00 ts1632back" $ n="ts1632back" $ m="18:45" $ echo ${line} | nawk -v a=$n -v b=$m '{if ($5==a) $2=m; print }' F 10 23:00 ts1632back $It should've... (2 Replies)
Discussion started by: aoussenko
2 Replies

5. Shell Programming and Scripting

Question regarding sed usage

I have a html file with the following content:- <font face=verdana color=#000000>108946</font> <font face=verdana color=#000000>234346</font> I want to format the values inside the font tag using thousand separator. I have the following command which can be used for adding thousand... (4 Replies)
Discussion started by: Yoda
4 Replies

6. AIX

How to monitor the IBM AIX server for I/O usage,memory usage,CPU usage,network..?

How to monitor the IBM AIX server for I/O usage, memory usage, CPU usage, network usage, storage usage? (3 Replies)
Discussion started by: laknar
3 Replies

7. HP-UX

how can I find cpu usage memory usage swap usage and logical volume usage

how can I find cpu usage memory usage swap usage and I want to know CPU usage above X% and contiue Y times and memory usage above X % and contiue Y times my final destination is monitor process logical volume usage above X % and number of Logical voluage above can I not to... (3 Replies)
Discussion started by: alert0919
3 Replies

8. Shell Programming and Scripting

Need help - from awk, sed experts

Hi , I have a file as below. Contents of the file are -------------------- aaaaaaaaaaa aaaaaaaaaaa aaaaaaaaaaa aaaaaaaaaaa aaaaaaaaaaa bbbbbbbbbbb ccccccccccc ddddddddddd aaaaaaaaaaa aaaaaaaaaaa aaaaaaaaaaa aaaaaaaaaaa aaaaaaaaaaa bbbbbbbbbbb (4 Replies)
Discussion started by: Srini75
4 Replies

9. Shell Programming and Scripting

Shell Coding question for any experts out there

Given this one long stream of data (all one line): <TransactionDetail><TransactionHeader><ErrorLogging>YES</ErrorLogging><HistoryLogging>YES</HistoryLogging><ErrorDetection>NO</ErrorD... (4 Replies)
Discussion started by: dfran1972
4 Replies

10. UNIX for Advanced & Expert Users

Experts Only! Hard Question Ahead!!!!

SunOS5.8 is a radical departure from SunOs4.X in many ways. one of the important differences is the handling of devices. Adding devices under SunOS4.x required a kernel reconfiguration, recompliation and reboot. Under SunOS5.X, this has changed with the ability to add some drivers on the fly.... (1 Reply)
Discussion started by: Foo49272
1 Replies
Login or Register to Ask a Question