using sed

John Kane jak9213 at helios.TAMU.EDU
Sat Dec 9 06:05:11 AEST 1989


In article <14404 at eerie.acsu.Buffalo.EDU>, kwon at sybil.cs.Buffalo.EDU (Thomas Kwon) writes:
> I'm a novice hacker who is trying to use 'sed' to extract "certain
> string" of "certain column" of "certain line" of a result of a "certain
> command". 
> Since that sounds too confusing, here is what I really mean. By typing
> 'ruptime' at the prompt, I get something like the following :
> 
>   antares       up 21+04:15,     4 users,  load 1.36, 1.50, 1.29
>   castor        up  1+05:20,     0 users,  load 0.00, 0.00, 0.00
>   deneb         up 42+23:50,     0 users,  load 0.00, 0.00, 0.00
>   gort          up 14+14:47,    21 users,  load 2.48, 1.77, 1.59
>   joey          up 14+14:42,     2 users,  load 0.00, 0.05, 0.26
>   marvin        up 14+15:03,    10 users,  load 1.43, 1.34, 1.12
>   sybil         up 14+11:28,    33 users,  load 2.83, 2.93, 2.44
>   wolf          up 70+20:41,     0 users,  load 1.46, 1.49, 1.28
> 
> I want to extract the string "1.34" which is in "column 8" of "line 6"
> and set that to a variable. How can this be done?
> So far, I've been experimenting with : 
> 
>      set string=`ruptime|sed  <Then I got stuck here>`

Well, 

I am not much of a sed user, but I like awk, so how about:

    set string=`ruptime | awk '$1 == "marvin" {print $8}'`

This is fairly short and quick. It looks for a line that has the
first field containing the word "marvin" (I assumed that this is what
you wanted). It then "prints" the 8th field, in this case "1.34".

Nice, short, and sweet. I bet someone could probably do better than
this.


 John Arthur Kane, Systems Analyst, Microcomputer Support and Training
 Texas A&M University, College Station, TX 77843  (409) 845-9999

 jak9213 at helios.tamu.edu     profs: x043jk at tamvm1.tamu.edu



More information about the Comp.unix.questions mailing list