🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.unix.shell
Article #1285

Re: Getting at the first char of a string in Bourne shell

#1285
From: 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!"=/
Technical Details
Message-ID: <1990Sep29.193617.25752@iwarp.intel.com>
Path: arc1-ca.pugleaf.net!utzoo!attcan!uunet!aplcen!uakari.primate.wisc.edu!zaphod.mps.ohio-state.edu!swrinde!ucsd!ucbvax!iwarp.intel.com!news
References: <1308@ncrwat.Waterloo.NCR.COM> <9737@jpl-devvax.JPL.NASA.GOV>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!uunet!aplcen!uakari.primate.wisc.edu!zaphod.mps.ohio-state.edu!swrinde!ucsd!ucbvax!iwarp.intel.com!news
From: merlyn@iwarp.intel.com (Randal Schwartz)
Newsgroups: comp.unix.shell
Subject: Re: Getting at the first char of a string in Bourne shell
Message-ID: <1990Sep29.193617.25752@iwarp.intel.com>
Date: 29 Sep 90 19:36:17 GMT
References: <1308@ncrwat.Waterloo.NCR.COM> <9737@jpl-devvax.JPL.NASA.GOV>
Sender: news@iwarp.intel.com
Reply-To: merlyn@iwarp.intel.com (Randal Schwartz)
Organization: Stonehenge; netaccess via Intel, Beaverton, Oregon, USA
Lines: 34
In-Reply-To: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall)