0

Here's a link

I referred this but I am not able to understand, how I can split: Testing123 into Testing and 123

temp=${str%%:*} #only if there was colon in between but in my case there isn't.

If there was a space b/w them I would have gotten it to work.

Update: I want to split letters and digits.

Character count would 5, TESTI, and then number would be 4.

so if it is TESTI123.

I want it to be var1 = testi, var2 = 123

var1=$(echo $STP | cut '[A-Z]' -f1)
var2=$(echo $STP | cut '[0-9]' -f1)

Even this didnt work, cut: [A-Z]: No such file or directory cut: [0-9]: No such file or directory

Any help would be appreciated.

Community
  • 1
  • 1
kyle
  • 103
  • 1
  • 8
  • You're confusing `bash` and `ksh` with shell scripts I assume by your question, they are two different shells. There's probably no need to be specific but be portable and use `#!/bin/sh` instead. – Jite Nov 11 '14 at 16:33
  • 1
    On what do you want to split? Boundary between letters and digits? Specific character count? – Anton Nov 11 '14 at 16:33
  • yes, I want to split letters and digits. Character count would 5, TESTI, and then number would be 4. so if it is TESTI123. I want it to be var1 = testi, var2 = 123 – kyle Nov 11 '14 at 16:35

2 Answers2

1

Using sed

echo testi1234 | sed 's/[0-9]*//g'
echo testi1234 | sed 's/[a-z]*//g'

More complex, putting a space between them,

echo testi1234 | sed 's/\([A-Za-z]*\)\([0-9]*\)/\1 \2/'  

EDIT: sed is not needed for the example the OP gives. Alternatives beneath.

In this case sed is to complex, you can just use tr :

echo testi1234 | tr -d "[a-zA-Z]"
echo testi1234 | tr -d "[0-9]"

When you want to use the internal shell syntax, you can also use

full=testi12345
start=${full%%[0-9]*}; echo ${start}
endstr=${full##*[[:lower:]]}; echo ${endstr} 

Internal evaluation works fast, but is difficult to understand.

Walter A
  • 19,067
  • 2
  • 23
  • 43
0

The answer to the following questions is: echo "testi1234" |awk 'BEGIN{FIELDWIDTHS="5 4"}{print $1 RS $2}'

Now just need to store $1 and $2 in different variable and should be good to go. If anybody else has better answer please comment, I am open to learning new concepts.

kyle
  • 103
  • 1
  • 8