[comp.unix.questions] using sed

kwon@sybil.cs.Buffalo.EDU (Thomas Kwon) (12/09/89)

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>`

Any help will be greatly appreciated.

                                                    Tom Kwon. *-)

jak9213@helios.TAMU.EDU (John Kane) (12/09/89)

In article <14404@eerie.acsu.Buffalo.EDU>, kwon@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@helios.tamu.edu     profs: x043jk@tamvm1.tamu.edu

jdpeek@rodan.acs.syr.edu (Jerry Peek) (12/11/89)

In article <14404@eerie.acsu.Buffalo.EDU> kwon@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?

It depends on how "ruptime" formats its output -- if a load average number is
over 9.99, is there still a leading space?  The way I've done it "plays safe"
by including all text between the two commas, with the leading space:

   marvin        up 14+15:03,    10 users,  load 1.43, 1.34, 1.12
						      ^^^^^

Here's the line I came up with:

	set load=`ruptime | sed -n '6s/^.*load[^,]*,\([^,]*\).*/\1/p'`

In English, that reads:
        -n              Don't print any line unless I give the "p" command
        6s/             On line 6, substitute...
        ^.*load[^,]*,   Everything up to and including the first comma (,)
                        after the word "load"
        \([^,]*\)       Save everything up to but not including the next
                        comma in the tagged field 1
        .*              The rest of the line
        /\1/            Replace all that stuff (the entire line) with the
                        contents of tagged field 1
        p               Print this line

If you'd rather have the line that contains the word "marvin" instead
of the 6th line, the sed expression should look like this:

    set load=`ruptime | sed -n '/marvin/s/^.*load[^,]*,\([^,]*\).*/\1/p'`

I'm no expert, but I think "sed" is great -- fast and flexible, just cryptic.
Maybe Randall and I should have a perl-vs.-sed showdown. :-) :-)

--Jerry Peek; Syracuse University Academic Computing Services; Syracuse, NY
  jdpeek@rodan.acs.syr.edu, JDPEEK@SUNRISE.BITNET        +1 315 443-3995

wiml@blake.acs.washington.edu (William Lewis) (12/18/89)

In article <14404@eerie.acsu.Buffalo.EDU> kwon@sybil.cs.Buffalo.EDU (Thomas Kwon) writes:
>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>`

  Alternatively you could use 'cut' to slice out the desired field.
Something along the lines of 

  set string=`ruptime|tail +6|head -1|cut -d"," -f4`

 should work (head and tail to get only that line; cut to get the
fourth comma-separated field.)
 Maybe

  set string=`ruptime|sed -n6p|cut -d"," -f4`

  would work better, come to think of it.

     --- phelliax 

-- 
wiml@blake.acs.washington.edu        (206)526-5885      Seattle, Washington