KSH: Accessing individual words in a variable string (like arrays)
KSH: Accessing individual words in a variable string (like arrays)
am 30.11.2007 02:38:05 von Generic Usenet Account
Kindly suggest the ksh equivalent of the following csh script snippet
##############################
set presidents=(Washington Adams Jefferson Madison Monroe Qunicy-Adams
Jackson)
echo $presidents[1] $presidents[4-7]
##############################
Re: KSH: Accessing individual words in a variable string (like
am 30.11.2007 09:49:56 von Janis Papanagnou
On 30 Nov., 02:38, use...@sta.samsung.com wrote:
> Kindly suggest the ksh equivalent of the following csh script snippet
>
> ##############################
> set presidents=(Washington Adams Jefferson Madison Monroe Qunicy-Adams
> Jackson)
>
> echo $presidents[1] $presidents[4-7]
> ##############################
What version of ksh are you using? Try one of...
set - Washington Adams Jefferson Madison Monroe Qunicy-Adams Jackson
echo $1 $4 $5 $6 $7
set - Washington Adams Jefferson Madison Monroe Qunicy-Adams Jackson
printf "%s" $1; shift 3; echo "$@"
set - Washington Adams Jefferson Madison Monroe Qunicy-Adams Jackson
echo ${@:1:1} ${@:4:4}
presidents=( Washington Adams Jefferson Madison Monroe Qunicy-Adams
Jackson )
echo ${presidents:1:1} ${presidents:4:4}
Janis
Re: KSH: Accessing individual words in a variable string (like
am 30.11.2007 10:27:52 von Janis Papanagnou
On 30 Nov., 09:49, Janis wrote:
> On 30 Nov., 02:38, use...@sta.samsung.com wrote:
>
> > Kindly suggest the ksh equivalent of the following csh script snippet
>
> > ##############################
> > set presidents=(Washington Adams Jefferson Madison Monroe Qunicy-Adams
> > Jackson)
>
> > echo $presidents[1] $presidents[4-7]
> > ##############################
>
> What version of ksh are you using? Try one of...
>
> set - Washington Adams Jefferson Madison Monroe Qunicy-Adams Jackson
> echo $1 $4 $5 $6 $7
>
> set - Washington Adams Jefferson Madison Monroe Qunicy-Adams Jackson
> printf "%s" $1; shift 3; echo "$@"
>
> set - Washington Adams Jefferson Madison Monroe Qunicy-Adams Jackson
> echo ${@:1:1} ${@:4:4}
>
> presidents=( Washington Adams Jefferson Madison Monroe Qunicy-Adams
> Jackson )
> echo ${presidents:1:1} ${presidents:4:4}
Oops. The latter should have been...
echo ${presidents[@]:1:1} ${presidents[@]:4:4}
>
> Janis
Re: KSH: Accessing individual words in a variable string (like
am 04.12.2007 01:52:22 von Generic Usenet Account
On Nov 29, 7:38 pm, use...@sta.samsung.com wrote:
> Kindly suggest the ksh equivalent of the following csh script snippet
>
> ##############################
> set presidents=(Washington Adams Jefferson Madison Monroe Qunicy-Adams
> Jackson)
>
> echo $presidents[1] $presidents[4-7]
> ##############################
The only thing that I can get to work is the following:
presidents[0]=Washington
presidents[1]=Adams
presidents[2]=Jefferson
presidents[3]=Madison
presidents[4]=Monroe
presidents[5]=Qunicy-Adams
presidents[6]=Jackson
echo ${presidents[0]} ${presidents[3]}
##### Could not find the KSH equivalent for echo
$presidents[4-7]
echo ${#presidents[*]}
Is there some way to print a range of array elements?
Thanks
[Courtesy: http://www.livefirelabs.com/unix_tip_trick_shell_script/nov_ 2003/11102003.htm
http://www.livefirelabs.com/unix_tip_trick_shell_script/nov_ 2003/11172003.htm]
Re: KSH: Accessing individual words in a variable string (like
am 04.12.2007 09:42:38 von Janis Papanagnou
On 4 Dez., 01:52, Generic Usenet Account
wrote:
> On Nov 29, 7:38 pm, use...@sta.samsung.com wrote:
>
> > Kindly suggest the ksh equivalent of the following csh script snippet
>
> > ##############################
> > set presidents=(Washington Adams Jefferson Madison Monroe Qunicy-Adams
> > Jackson)
>
> > echo $presidents[1] $presidents[4-7]
> > ##############################
>
> The only thing that I can get to work is the following:
>
> presidents[0]=Washington
> presidents[1]=Adams
> presidents[2]=Jefferson
> presidents[3]=Madison
> presidents[4]=Monroe
> presidents[5]=Qunicy-Adams
> presidents[6]=Jackson
>
> echo ${presidents[0]} ${presidents[3]}
> ##### Could not find the KSH equivalent for echo
> $presidents[4-7]
>
> echo ${#presidents[*]}
>
> Is there some way to print a range of array elements?
See my reply upthread...
In ksh93, bash, ... (otherwise use the 'set - ...' approach)
echo ${presidents[@]:1:1} ${presidents[@]:4:4}
The second second number defines the number of elements (if omitted
until the end of the array list)... ${arrayvar[@]:index:length}
Janis
> Thanks
>
> [Courtesy:http://www.livefirelabs.com/unix_tip_trick_shell_s cript/nov_2003/1110...
>
> http://www.livefirelabs.com/unix_tip_trick_shell_script/nov_ 2003/1117...]