Path: 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 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 --