Korn Shell Krankiness

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???

This entry was posted in computers/programming. Bookmark the permalink.

5 Responses to Korn Shell Krankiness

  1. bturnip says:

    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.


    cut -b3-18 my_input.txt > my_output.txt

    Man, that is simple!

    Here’s a Perl script that does the same thing with a whole lot more code:

    #!/usr/bin/perl -w
    
    # open files
    open(INPUT,"my_input.txt")   || die "cannot open input file";
    open(OUTPUT, "> my_output.txt") || die "cannot open output file";
    
    while()
      {
       # extract the desired substring
       # add a newline to the end
       $snip=substr($_,3,18) . "n";
       # pump the new string into the output file
       print OUTPUT $snip; 
      }
    close(INPUT);
    close(OUTPUT);
    
    
    exit;
    

  2. Jon Kreisler says:

    The ksh93 version of ksh DOES support the syntax:

    foo=teststring
    bar=${foo:0:5}
    echo $bar

    see http://www.kornshell.com/

  3. Vij says:

    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.

  4. Vij says:

    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.

  5. Vij says:

    awk: Input line : execut cannot be longer than 3,000 bytes.

Leave a Reply

Your email address will not be published. Required fields are marked *