本文介绍了我想在另一个字符串模式前面找到一些字符串,该怎么办?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用bash shell分割字符串,例如:
I want to use bash shell to split string like:
Calcipotriol - Daivonex Cream 50mcg/1g 30 g [1]
Aspirin - DBL Aspirin 100mg [1] tablet
我想获得商标名称"Davionex Cream"和"DBL Aspirin"我想把名字放在*** mg或*** mcg或*** g前面
I want to get brand name "Davionex Cream" and "DBL Aspirin"I want to get the name in front of parttern ***mg or ***mcg or ***g
该怎么做?
推荐答案
在Bash中,您可以执行以下操作:
In Bash you can do:
while IFS= read -r line || [[ -n "$line" ]]; do
if [[ "$line" =~ ^([[:alpha:]]+)[[:space:][:punct:]]+([[:alpha:][:space:]]+)[[:space:]](.*)$ ]]
then
printf "1:'%s' 2:'%s' 3:'%s'\n" "${BASH_REMATCH[1]}" "${BASH_REMATCH[2]}" "${BASH_REMATCH[3]}"
fi
done <<<"Calcipotriol - Daivonex Cream 50mcg/1g 30 g [1]
Aspirin - DBL Aspirin 100mg [1] tablet"
打印:
1:'Calcipotriol' 2:'Daivonex Cream' 3:'50mcg/1g 30 g [1]'
1:'Aspirin' 2:'DBL Aspirin' 3:'100mg [1] tablet'
这篇关于我想在另一个字符串模式前面找到一些字符串,该怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!