Re: splitting words into individual characters
On 2006-11-04, Mitch wrote:
> [...]
> >> I tried to get this to work but just had no luck with it as is.
>>
>> What does "no luck" mean? Please be specific.
>
> You know what, I don't know. I have since restarted my PuTTY session and
> it works fine. Maybe it was a cut and paste error. maybe I wasn't
> referencing the file properly. It works fine now, thank you.
>
> [...]
>
>> Are you using [t]csh? If so, Bourne-shell syntax will not work.
>
> GNU bash version 2.05a.0(1), but like i said, is working fine now.
With bash (version 2 or greater), you can access individual
characters with parameter expansion:
$ string=drwxr-x---
$ echo "${string:0:1}"
d
$ echo "${string:5:2}"
-x
>>> That said, without the other bits works a treat,so thank you. I was
>>> just curious why the outer parts were added.
>>
>> The script places each permissions character in a positional
>> parameter, so that they can be referred to by number:
>> $1, $2, ... ${10}.
>
> That is very useful. that's something I've learned that I'll use again!
>
> As for the command itself:
>
> 's/./& /g'
>
> therefore must find every character and replace it with itself and a
> space. That I get, however
>
> 's/ .*//' stumps me a little...
>
> I know it cuts all but the first term, but was curious how. does it
> replace anything with a space before it with nothing? that would be my
> guess on first glance, but would appreciate some confirmation.
/ .*/ matches a space followed by zero or more instances of any
character; the expression is "greedy", so it will match everything
to the end of the line. IOW, it matches everything from the first
space to the end of the line, and that is deleted.
--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
|