Article View: comp.unix.shell
Article #1290Re: Getting at the first char of a string in Bourne shell
From: Mark Sicignano
Date: Sun, 30 Sep 1990 16:12
Date: Sun, 30 Sep 1990 16:12
46 lines
1542 bytes
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
--
Technical Details
Message-ID:
<2163@hsi86.hsi.UUCP>Path:
arc1-ca.pugleaf.net!utzoo!attcan!utgpu!news-server.csri.toronto.edu!mailrus!wuarchive!uunet!hsi!markReferences:
<1308@ncrwat.Waterloo.NCR.COM> <9737@jpl-devvax.JPL.NASA.GOV> <1990Sep29.193617.25752@iwarp.intel.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!utgpu!news-server.csri.toronto.edu!mailrus!wuarchive!uunet!hsi!mark From: mark@hsi.UUCP (Mark Sicignano) Newsgroups: comp.unix.shell Subject: Re: Getting at the first char of a string in Bourne shell Message-ID: <2163@hsi86.hsi.UUCP> Date: 30 Sep 90 16:12:24 GMT References: <1308@ncrwat.Waterloo.NCR.COM> <9737@jpl-devvax.JPL.NASA.GOV> <1990Sep29.193617.25752@iwarp.intel.com> Reply-To: mark@hsi.com (Mark Sicignano) Organization: 3M Health Information Systems, Wallingford, CT. Lines: 46