The UNIX boxes I use at work right now are all AIX machines using the Korn shell. I have been writing little shell scripts to take care of all my little tasks. It has been fun and worthwhile- one shell script I put together does it work in about 5 minutes, compared to the hour it was taking before. So handy, except when it comes to substrings.
I have been completely frustrated trying to extract substrings using ksh built-in tools. In my perfect world, I could use this inside my .ksh script:
foo='Hello World!'
bar=substr($foo,0,5)
print $bar
Running said code would make “Hello
” print to the screen. Malheureusement, ce n’est pas le cas.
Having to revert to some documentation, it would seem that I would be able to use the variable expansion tools of ksh:
foo='Hello World!'
bar=${foo:0:5}
print $bar
Does this give me what I want? No, it gives me what I do not want-it gives a maddeningly-almost-clear explanation:
ksh: ${foo:0:5}: 0403-011 The specified substitution is not valid for this command.
Why, Korn Shell, why???
I went home to play with my Fedora Core 3 system with the bash shell. I was relieved to find that the variable expansion as listed in the main article does work in the Bash shell, i.e.:
foo=teststring
bar=${foo:1:5}
echo $bar
This will indeed give me what I expect, printing $bar gives me “estst”.
For my script at work, I approached the problem from a different angle. Instead of screwing around with variable expansion or Korn shell built-ins, I decided to use the
cut
command. Back to basics.Dead simple, even better than using Perl to get my cherished familiar
substr()
function.Man, that is simple!
Here’s a Perl script that does the same thing with a whole lot more code:
The ksh93 version of ksh DOES support the syntax:
foo=teststring
bar=${foo:0:5}
echo $bar
see http://www.kornshell.com/
in Korn shell substring function : error :
The specified substitution is not valid for this command
shows up again and again!!! Bash is much better than ksh.
when trying to extract substring using awk, it throws up error : can not process more than 3000 bytes.
You can rely upon sed or `cut -c 1-80` commands to extract substring when bash is not available.
awk: Input line : execut cannot be longer than 3,000 bytes.