[nSLUG] Extracting first char from shell var

D G Teed donald.teed at gmail.com
Thu Feb 13 15:58:04 AST 2014


I had tried with ^ as well.  There must be some shell
or environment basis for this.  I've run the same from
a Debian desktop shell and had the expected result.
I'm copying and pasting the same command
between session terms.

Redhat 5.9 bash:

$ bash --version
GNU bash, version 3.2.25(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2005 Free Software Foundation, Inc.

$ echo $mailuser
testuser
$ echo $mailuser | grep -Eo '^[a-z]{1}'
t
e
s
t
u
s
e
r

On Debian 7:

$ bash --version
GNU bash, version 4.2.37(1)-release (i486-pc-linux-gnu)
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html
>



$ export mailuser=testuser
$ echo $mailuser | grep -Eo '^[a-z]{1}'
t





On Thu, Feb 13, 2014 at 3:03 PM, Dop Ganger <nslug at fop.ns.ca> wrote:

> It's matching up to one character. Try:
>
> for i in `seq 1 100` ; do echo testuser | grep -Eo '[a-z]{'${i}'}' ; done
>
> vs
>
> for i in `seq 1 100` ; do echo testuser | grep -Eo '^[a-z]{'$i'}' ; done
>
> vs
>
> for i in `seq 1 100` ; do echo testuser | grep -Eo '[a-z]{'$i'}$' ; done
>
> Does that help explain what's going on? I picked 100 as an arbitrarily
> large number that wasn't too large.
>
> Cheers... Dop.
>
>
> On Thu, 13 Feb 2014, D G Teed wrote:
>
>
>> Say I've got a username in a shell variable
>> mailuser with the value "testuser".  I want to extract
>> the first letter in the user name because it
>> is needed to walk the cyrus mail directory tree.
>>
>> So one answer is:
>>
>> echo ${mailuser:0:1}
>> t
>>
>> Now that we've got that out of the way, I'm curious
>> what is happening in this:
>>
>> echo $mailuser | grep -Eo '[a-z]{1}'
>> t
>> e
>> s
>> t
>> u
>> s
>> e
>> r
>>
>> Same without the pipe:
>>
>> cat /tmp/testfile
>> testuser
>>
>> grep -Eo '[a-z]{1}' /tmp/testfile
>> t
>> e
>> s
>> t
>> u
>> s
>> e
>> r
>>
>>
>> Whereas I've used patterns in -Eo to extract IP addresses,
>> and this works as expected:
>>
>> grep -Eo ' [a-z]{9}$' /etc/hosts
>>  localhost
>>
>>
>>
>>
> _______________________________________________
> nSLUG mailing list
> nSLUG at nslug.ns.ca
> http://nslug.ns.ca/mailman/listinfo/nslug
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://nslug.ns.ca/pipermail/nslug/attachments/20140213/f05017da/attachment.html>


More information about the nSLUG mailing list