cropping a string

cropping a string

Post by Ravi Kum » Wed, 07 Feb 1996 04:00:00




Quote:>$long = "Brent is logged in".

>I want a new variable whose value is just the first word of that file:

>$short = "Brent".

>What can I do to $long to get $short?

>--Brent

short=`echo $long | awk '{print $1}'` (My choice..)

or

short=`echo $long | cut -d" " -f1`

- Ravi

 
 
 

cropping a string

Post by Sylvain Senech » Wed, 07 Feb 1996 04:00:00



Quote:>$long = "Brent is logged in".
>I want a new variable whose value is just the first word of that file:
>$short = "Brent".
>What can I do to $long to get $short?

Try the following:

short='echo $long | awk '{print $1}''

      |                             |
      +-----------------------------+----- Are Backquote.

Truly,

Sylvain Senechal

Take a look at: http://www.speedware.com

 
 
 

cropping a string

Post by Scott Stanchfiel » Thu, 08 Feb 1996 04:00:00


Couldn't this just be done as

(in c-shell)
#! /bin/csh

set long="Brent was here"
set short=${long[1]}
echo $long
echo $short

(something similar should be doable in sh or ksh...)

-- Scott



> >$long = "Brent is logged in".

> >I want a new variable whose value is just the first word of that file:

> >$short = "Brent".

> >What can I do to $long to get $short?

> >--Brent

> short=`echo $long | awk '{print $1}'` (My choice..)

> or

> short=`echo $long | cut -d" " -f1`

> - Ravi

--
Scott Stanchfield     McCabe & Associates -- Columbia, Maryland
(These are not necessarily the opinions of McCabe & Associates)
      Visit McCabe & Associates at http://www.mccabe.com
 
 
 

cropping a string

Post by Phil Abercromb » Fri, 09 Feb 1996 04:00:00


   A question:

   Suppose I have a variable whose value is a line-long string:

   $long = "Brent is logged in".

   I want a new variable whose value is just the first word of that file:

   $short = "Brent".

   What can I do to $long to get $short?

   --Brent

I would suggest the following

$ echo $long | read short junk

However, it doesn't seem to work in sh on solaris, so instead I
suggest the following:

$ set -- $long; short=$1

--
 Phil Abercrombie         |   The nail which |    __O   | Bike

 +64 4 496 9000 x 797     | be hammered flat | (*)/-(*) | Work

 
 
 

cropping a string

Post by Tom Sande » Fri, 09 Feb 1996 04:00:00



|> >
|> >Suppose I have a variable whose value is a line-long string:
|> >
|> >$long = "Brent is logged in".
|> >
|> >I want a new variable whose value is just the first word of that file:
|> >
|> >$short = "Brent".
|>
|> I usually use awk for this kind of field extraction:
|>
|>     $ short=`echo $long | awk '{ print $1 }'`
|>
|> --
|> John Caruso, Senior Technical Consultant
|> ADP Claims Solutions Group                 Phone: (800) 366-4237 x2102
|> 2010 Crow Canyon Place                     FAX  : (510) 866-4839

|>

A much faster way is to use cut:  short=`echo $long | cut -f1 -d" "`
This sets short to field 1 of long using space as a delimiter.

Tom Sanders

 
 
 

cropping a string

Post by Tony Manci » Fri, 16 Feb 1996 04:00:00


: > Suppose I have a variable whose value is a line-long string:
: >
: > $long = "Brent is logged in".
: > I want a new variable whose value is just the first word of that file:
: > $short = "Brent".
: > What can I do to $long to get $short?

The best way I've seen to do this is in ksh with this:

short=${long%% *}

To just remove the last word, you could have used:

lastword=${long %*}     # delete from R -> L up to first space


 
 
 

cropping a string

Post by Robin Maddoc » Fri, 16 Feb 1996 04:00:00



> Suppose I have a variable whose value is a line-long string:

> $long = "Brent is logged in".
> I want a new variable whose value is just the first word of that file:
> $short = "Brent".
> What can I do to $long to get $short?

short=`echo $long | awk '{print $1}'`

(in bourne shell)

This works, but there is probably a nicer way to do it.

            Robin Maddock

 
 
 

cropping a string

Post by Dan Merc » Sat, 17 Feb 1996 04:00:00


: > Suppose I have a variable whose value is a line-long string:
: >
: > $long = "Brent is logged in".
: > I want a new variable whose value is just the first word of that file:
: > $short = "Brent".
: > What can I do to $long to get $short?
: >

: short=`echo $long | awk '{print $1}'`

: (in bourne shell)

: This works, but there is probably a nicer way to do it.

:             Robin Maddock
$ long="Brent is logged in"
$ for short in $long; { break;}
$ echo $short
Brent

Alternatively:
$ long="Brent is logged in"
$ for short in $long; do break;done
$ echo $short
Brent

To get the last item:
$ long="Brent is logged in"
$ for last in $long; do :;done
$ echo $last
in

Alternatively:
$ long="Brent is logged in"
$ for last in $long;{ :;}
$ echo $last
in
--
Dan Mercer

Opinions expressed herein are my own and may not represent those of 3M.

 
 
 

cropping a string

Post by John Dunnin » Mon, 19 Feb 1996 04:00:00



> Suppose I have a variable whose value is a line-long string:

> $long = "Brent is logged in".
> I want a new variable whose value is just the first word of that file:
> $short = "Brent".
> What can I do to $long to get $short?

short=`echo $long | awk '{print $1}'`

try

        set $long
        short=$1

 
 
 

1. cropping a string

: A question:
: Suppose I have a variable whose value is a line-long string:
: $long = "Brent is logged in".
: I want a new variable whose value is just the first word of that file:
: $short = "Brent".
: What can I do to $long to get $short?
: --Brent

Simple:
IFS=' '         # change internal separator character to just a space.
set -- $long    # force shell word separation
echo short = $1

--
   ,---.     ,---.     ,---.     ,---.     ,---.     ,---.     ,---.  
  /  _  \   /  _  \   /  _  \   /  _  \   /  _  \   /  _  \   /  _  \  

__,'   `.___,'   `.___,'   `.___,'   `.___,'   `.___,'   `.___,'   `.__

2. blanks on each line

3. utlilty for cropping ps files

4. Redhat 7.3 and Graphic cards with Kyro chipset

5. ps command cropping output

6. How to boot from a floppy

7. Selecting from the crop of DSL hub/switch/firewall units

8. vmstat and multiple cpu's

9. Batch crop a single large image to many smaller ones?

10. Apache logs are being cropped

11. Gimp: How to crop image?

12. wwwwais Errors Just Cropped Up

13. CGI output cropped in Apache?