如何在shell中分割字符串 [英] How to split a string in shell
本文介绍了如何在shell中分割字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个变量
string="ABC400p2q4".
如何分隔 ABC400
和 p2q4
.我需要将其分成两个变量,这样我就可以得到
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
可以使用任何字母字符代替ABC.代替400可以有任何其他数字;但是 p
和 q
是固定的,并且也可以代替2和4.
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.
推荐答案
由于它们是固定长度的子字符串,因此无需基于正则表达式模式进行拆分.在纯 bash
中,您可以执行以下操作:
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
这篇关于如何在shell中分割字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文