Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split a string in shell

I have a variable as

string="ABC400p2q4".

how can I separate ABC400 and p2q4. I need to separate it in two variables in such a way that as a result I get

echo $var1
ABC400
echo $var2
p2q4

In place of ABC there can be any alphabetic characters; in place of 400 there can be any other digits; but p and q are fixed and in place of 2 and 4 as well there can be any digit.

like image 210
XYZ_Linux Avatar asked Dec 04 '22 12:12

XYZ_Linux


1 Answers

No need to split based on a regexp pattern as they are fixed length substrings. In pure bash you would do:

$ string="ABC400p2q4"

$ var1=${string:0:6}

$ var2=${string:6}

$ echo $var1
ABC400

$ echo $var2
p2q4
like image 62
Chris Seymour Avatar answered Dec 24 '22 09:12

Chris Seymour