0

I have a SESSION_TOKEN which gets generated dynamically every 30 mins. Its character length is greater than 530 and approximately 536 characters will be there in it.

How can i split this string in UNIX scripting. Need help.

4
  • Its a bash script .Any suggestion on this? Commented Oct 13, 2014 at 11:46
  • How do you mean split? By delimiter? By length? Commented Oct 13, 2014 at 12:51
  • By length. Say i want to split the huge string into 3 variables based on length. 1 to 150, 151 to 300, 301 to end of string Commented Oct 13, 2014 at 14:12
  • 1
    Please edit the question (just click edit below the question), to get all the information together. Comments will get lost along the way. Commented Oct 13, 2014 at 21:33

2 Answers 2

1

You can use the "cut" utility for this kind of fixed length work:

echo "AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHHIIIIJJJJKKK" | cut -c 10-20
CCCDDDDEEEE

The -c means "select by character" and the "10-20" says which characters to select.

You can also select by byte (using -b) which might make a difference if your data has some unusual encoding.

In your case, where you want to do multiple chunks of the same string, something like:

bradh@saxicola:~$ export somethingToChop="AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHHIIIIJJJJKKK"
bradh@saxicola:~$ echo $somethingToChop | cut -c 1-10
AAAABBBBCC
bradh@saxicola:~$ echo $somethingToChop | cut -c 11-20
CCDDDDEEEE
bradh@saxicola:~$ echo $somethingToChop | cut -c 20-
EFFFFGGGGHHHHIIIIJJJJKKK

Would probably be the easiest to understand.

Sign up to request clarification or add additional context in comments.

1 Comment

Can you tick the "mark as answer" if you're OK with this answer.
1

Bash variable expansion has substring operations built in:

$ string="abcdefghijklmnopqrstuvwxyz";
$ first=${string:0:8}
$ second=${string:8:8}
$ third=${string:16}

$ echo $first, $second, $third
abcdefgh, ijklmnop, qrstuvwxyz

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.