🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.unix.shell
7 messages
7 total messages Started by Ken Braithwaite Thu, 27 Sep 1990 21:09
Getting at the first char of a string in Bourne shell
#1273
Author: Ken Braithwaite
Date: Thu, 27 Sep 1990 21:09
11 lines
874 bytes
I am trying to peel off one at a time the letters from a variable
in the Bourne shell, ie from Bourne get B and ourne.
How can I do this?
Thanks in advance.

----------------------------------------------------------------------
Ken Braithwaite            | They couldn't hit an elephant at
                           |   this dist
These opinions are solely  |       last words, General Sedgwick
my own. I don't share.     | 
----------------------------------------------------------------------
Re: Getting at the first char of a string in Bourne shell
#1276
Author: Larry Wall
Date: Fri, 28 Sep 1990 23:59
23 lines
1069 bytes
In article <1308@ncrwat.Waterloo.NCR.COM> ken@images1.Waterloo.NCR.COM (Ken Braithwaite) writes:
: I am trying to peel off one at a time the letters from a variable
: in the Bourne shell, ie from Bourne get B and ourne.
: How can I do this?

A fairly efficient way would be

    echo "Bourne" | SOMETHING | while read line; do
	...
    done

where SOMETHING is one of

    sed 's/\(.\)/\1\
    /g
    s/\n$//'

    perl -pe 'chop; s/./$&\n/g'

There are lots of other ways.

Larry Wall
lwall@jpl-devvax.jpl.nasa.gov
Re: Getting at the first char of a string in Bourne shell
#1285
Author: Randal Schwartz
Date: Sat, 29 Sep 1990 19:36
34 lines
1917 bytes
In article <9737@jpl-devvax.JPL.NASA.GOV>, lwall@jpl-devvax (Larry Wall) writes:
|     perl -pe 'chop; s/./$&\n/g'
| 
| There are lots of other ways.

Ack.  Just to even out the "do everything in Perl" reputation I've
gotten, here's how to do it *without* Perl.  (And I'm even doing this
as a followup to Larry's article... sheesh.  If that doesn't even out
the score, I don't know what will!)

a="Bourne" # pretend you already have it in $a
first=`expr "$a" : '\(.\).*'` # to get the first char
a=`expr "$a" : '.\(.*\)` # to trim the first char off
echo "$first $a"

[no whitespace in $a, please]

or even:

a="Bourne" # pretend you already have it in $a
eval `echo "$a" | sed 's/\(.\)\(.*\)/first=\1 a="\2"/'`
echo "$first $a"

[This requires well-behaved values of $a... whitespace is OK, quoting
characters are out.]

There.  Not a single 'perl' invocation in the pile :-)

Just another Bourne-shell hacker,
-- 
/=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\
| on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III      |
| merlyn@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn |
\=Cute Quote: "Welcome to Portland, Oregon, home of the California Raisins!"=/
Re: Getting at the first char of a string in Bourne shell
#1286
Author: Pim Zandbergen
Date: Sat, 29 Sep 1990 21:55
15 lines
952 bytes
How about 

string=`echo $string | cut -c1`

I don't think the Bourne shell can do this without executing
an external command.

In Korn shell you can use a hack like

string=${string%${string#?}}
-- 
Pim Zandbergen                          domain : pim@cti-software.nl
CTI Software BV                         uucp   : uunet!mcsun!hp4nl!ctisbv!pim
Laan Copes van Cattenburch 70           phone  : +31 70 3542302
2585 GD The Hague, The Netherlands      fax    : +31 70 3512837
Re: Getting at the first char of a string in Bourne shell
#1290
Author: Mark Sicignano
Date: Sun, 30 Sep 1990 16:12
46 lines
1542 bytes
In article <9737@jpl-devvax.JPL.NASA.GOV>, lwall@jpl-devvax (Larry Wall) writes:
 >     perl -pe 'chop; s/./$&\n/g'
 > 
 > There are lots of other ways.
 >

In article <1990Sep29.193617.25752@iwarp.intel.com> merlyn@iwarp.intel.com (Randal Schwartz) writes:
 >
 >a="Bourne" # pretend you already have it in $a
 >first=`expr "$a" : '\(.\).*'` # to get the first char
 >a=`expr "$a" : '.\(.*\)` # to trim the first char off
 >echo "$first $a"
 >
 >[no whitespace in $a, please]
 >
 >or even:
 >
 >a="Bourne" # pretend you already have it in $a
 >eval `echo "$a" | sed 's/\(.\)\(.*\)/first=\1 a="\2"/'`
 >echo "$first $a"
 >

Double ack!  These ways might work, but what about readability!

echo "Bourne" | awk '
{
	for (i = 0; i < length; i++) {
		print substr($0, i + 1, 1);
	}
}'

Produces:
B
o
u
r
n
e

That's what you want, right?

-mark
-- 
Mark Sicignano                                  ...!uunet!hsi!mark
3M Health Information Systems                   mark@hsi.com
--
Re: Getting at the first char of a string in Bourne shell
#1292
Author: Scott Yelich
Date: Mon, 01 Oct 1990 03:21
27 lines
1795 bytes
>I don't think the Bourne shell can do this without executing
>an external command.

Uh, it CAN be done... the question is whether you want to fork another
process or have a massive hack to accomplish something which is
actually a minimal function (Lets pass a lisp function to get the
first letter to emacs... etc).

As an EXAMPLE only, and an example which YOU can expand...

STRING="STRING"
set +F
case "$STRING" in
  S* ) FIRST=S;;
esac
echo "The first letter of \`\`$STRING'' is \`\`$FIRST\'\'

Now, do you REALLY want to do this?
--
Signature follows. [Skip now]

 -----------------------------------------------------------------------------
 Scott D. Yelich                         scott@[xanth.]cs.odu.edu [128.82.8.1]
 After he pushed me off the cliff, he asked me, as I fell, ``Why'd you jump?''
 Administrator of:    Game-Design requests to <game-design-request@cs.odu.edu>
 ODU/UNIX/BSD/X/C/ROOT/XANTH/CS/VSVN/
 -----------------------------------------------------------------------------
Re: Getting at the first char of a string in Bourne shell
#1298
Author: C C Evert
Date: Mon, 01 Oct 1990 20:39
16 lines
1073 bytes
In article <1308@ncrwat.Waterloo.NCR.COM>, ken@images1.Waterloo.NCR.COM (Ken Braithwaite) writes:
> I am trying to peel off one at a time the letters from a variable
> in the Bourne shell, ie from Bourne get B and ourne.
> How can I do this?
> Thanks in advance.
> 

expr "$SHELL" : '\(.\).*' prints the first letter of SHELL
expr "$SHELL" : '.\(.*\)' prints all but the first letter of SHELL

You still need to check for SHELL being empty.
-- 
C C Evert 
Western Geophysical - A division of Western Atlas International,
A Litton/Dresser Company           DOMAIN addr: cevert@airgun.wg.waii.com
				   UUNET address:  uunet!airgun!cevert
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads