String Split based on position
Posted on 2005-04-26
Using bash scripting (#!/bin/bash GNU)
VAR="8gbwirbfvrw8ovhnrogv4" ; # no spaces or new lines folks, could be any alphanumeric string
Split the string at position 5 ($SPLIT_NUM)
so the output of echo $RESULT should be "8gbwi"
unless we change SPLIT_NUM="10" so then the output would be "8gbwirbfvr"
or whatever the string is.
My thoughts on # ? section:
I could echo out $VAR to a new file and split with (no pun intended) "split" but that's not really splitting a variable, it's splitting a file.
I was thinking that since sed allows you to use "." for characters, and ^ for first posistion you could just delete the rest of the string, which is fine by me, but I am not knowledgable enough to get this fine tuned enough if $SPLIT_NUM changes.
I know it can be done in perl, but I need this in a bash script, so maybe awk it? Or does grep allow you to get the first x number of characters in a string and ignore the rest?