sh/ksh replacement for the following bash command

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

sh/ksh replacement for the following bash command

Michael Lechtermann-4
Hi,

I am looking for sh/ksh replacement for the following bash command:

$ X="abcdefghi"
$ echo ${X:0:2}
ab


Anyone got an idea?


Michael

Reply | Threaded
Open this post in threaded view
|

Re: sh/ksh replacement for the following bash command

Han Boetes
Michael wrote:
> I am looking for sh/ksh replacement for the following bash command:
>
> $ X="abcdefghi"
> $ echo ${X:0:2}
> ab

cut(1)



# Han

Reply | Threaded
Open this post in threaded view
|

Re: sh/ksh replacement for the following bash command

Michael Lechtermann-4
Hi,

Han Boetes schrieb:
> Michael wrote:
>> I am looking for sh/ksh replacement for the following bash command:
>>
>> $ X="abcdefghi"
>> $ echo ${X:0:2}
>> ab
>
> cut(1)

Thanks a lot, sometimes the obvious solutions are the hardest to find...

$ X="abcdefghi"
$ echo $X | cut -c 1-2
ab

:-)


Michael

Reply | Threaded
Open this post in threaded view
|

Re: sh/ksh replacement for the following bash command

Paul de Weerd
On Fri, Feb 08, 2008 at 05:03:40PM +0100, Michael wrote:
| Hi,
|
| Han Boetes schrieb:
| > Michael wrote:
| >> I am looking for sh/ksh replacement for the following bash command:
| >>
| >> $ X="abcdefghi"
| >> $ echo ${X:0:2}
| >> ab
| >
| > cut(1)
|
| Thanks a lot, sometimes the obvious solutions are the hardest to find...
|
| $ X="abcdefghi"
| $ echo $X | cut -c 1-2
| ab

Note that it's not a complete replacement for bash's
${parameter:offset:length} parameter expansion implementation. If you
give it @ or an array as a parameter, behaviour is completely
different.

Cheers,

Paul 'WEiRD' de Weerd

--
>++++++++[<++++++++++>-]<+++++++.>+++[<------>-]<.>+++[<+
+++++++++++>-]<.>++[<------------>-]<+.--------------.[-]
                 http://www.weirdnet.nl/                 

Reply | Threaded
Open this post in threaded view
|

Re: sh/ksh replacement for the following bash command

Aner Perez
In reply to this post by Michael Lechtermann-4
Michael wrote:

> Hi,
>
> I am looking for sh/ksh replacement for the following bash command:
>
> $ X="abcdefghi"
> $ echo ${X:0:2}
> ab
>
>
> Anyone got an idea?
>
>
> Michael
>
How about this:

$ echo ${X%${X#??}}
ab

${X#??} is $X with the first 2 characters (??) removed.

${X%ZZZZZ} is $X with ZZZZZ removed from the end

So we removed from the end everything except the first 2 characters.  Sort of
convoluted but it works.

        - Aner

Reply | Threaded
Open this post in threaded view
|

Re: sh/ksh replacement for the following bash command

Christian Weisgerber
In reply to this post by Michael Lechtermann-4
Michael <[hidden email]> wrote:

> I am looking for sh/ksh replacement for the following bash command:
>
> $ X="abcdefghi"
> $ echo ${X:0:2}
> ab

echo ab

Yes, I'm being facetious, but without context it's not clear what an
appropriate "replacement" would be.  Traditionally, the regular
expression operator of the expr(1) command has been used for string
processing in shell scripts:

expr "$X" : '\(.\{0,2\}\)'

Frequently, the POSIX sh(1) substring processing to remove the
smallest/largest suffix/prefix pattern is sufficient.

  ${name#pattern}
  ${name##pattern}
  ${name%pattern}
  ${name%%pattern}

--
Christian "naddy" Weisgerber                          [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: sh/ksh replacement for the following bash command

Michael Lechtermann-4
In reply to this post by Paul de Weerd
Hi,

Paul de Weerd schrieb:
> | > Michael wrote:
> | $ X="abcdefghi"
> | $ echo $X | cut -c 1-2
> | ab
>
> Note that it's not a complete replacement for bash's
> ${parameter:offset:length} parameter expansion implementation. If you
> give it @ or an array as a parameter, behaviour is completely
> different.

Actually, the above is just what I was looking for. :-)


Michael