String Split based on position

Posted on 2005-04-26
Last Modified: 2013-12-26
Using bash scripting (#!/bin/bash GNU)

VAR="8gbwirbfvrw8ovhnrogv4" ; # no spaces or new lines folks, could be any alphanumeric string
# ?
echo $RESULT

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?


Question by:ALEx604
    LVL 38

    Accepted Solution

    You can do:

    PART1=`echo $VAR | cut -c1-${SPLIT_NUM}`
    PART2=`echo $VAR | cut -c${SPLIT_NUM}-`

    You need to check the string length, before doing the split, eg

    if [ ${SPLIT_NUM} -le ${#VAR} ] ; then
        PART1=`echo $VAR | cut -c1-${SPLIT_NUM}`
        PART2=`echo $VAR | cut -c${SPLIT_NUM}-`
        echo "Position outside the string length!"

    LVL 2

    Author Comment

    # ?
    RESULT=`perl chars = split(//, $VAR) | for NEXT in $chars ; do if [ $SPLIT_NUM > 0 ] then echo $NEXT ; fi ; done`

    needs tweeking, just another idea


    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now