0

I have potential inputs that will come in from a read -e -p command in a bash script. For example, the user would type L50CA. Some other possibilites that the user could type in are: K117CB, K46CE2, or V9CE1.

I need to break up what was read in. I read in like this:

read -e -p "What first atom? " sel1

then I would like to make an array like this (but this will not separate):

arr1=($sel1)

But I need to separate the array so that

${arr1[0]} is equal to L ${arr1[1]} is equal to 50 and ${arr1[2]} is equal to CA

This separation has to work with the other possible user input formats like the ones listed above. Regex seems to be the way to do this. I can isolate the first two matches of the input with the following regular expressions: ^\D and \d*(?=\w)

I need help matching the third component and implementing it into an array. Alternatively, it is fine to to break up the user input into three new variables. Or we can place a space between each of the matches so L50CA is converted to L 50 CA because then arr1=($sel1) will work.

Thanks for your help.

2
  • What do K46CE2 split into? K, 46, CE2? Or K, 46, CE, 2? Commented Sep 21, 2014 at 22:41
  • It would split into K 46 CE2. It is fine if they come out separate like they did with jm66's answer because it is trivial to just combine the 3rd and 4th entry. Commented Sep 21, 2014 at 22:43

3 Answers 3

2

Bash only solution:

for sel in L50CA K117CB K46CE2 V9CE1; do
    [[ "$sel" =~ "^(\w)([0-9]+)(.*)" ]]
    printf '%s - ' "${BASH_REMATCH[@]}"
    printf \\n;
done
Sign up to request clarification or add additional context in comments.

Comments

1

The

for sel in L50CA K117CB K46CE2 V9CE1
do
        arr=($(sed 's/\([0-9][0-9]*\)/ \1 /g'<<<"$sel"))
        echo "${arr[@]}"
done

prints

L 50 CA
K 117 CB
K 46 CE 2
V 9 CE 1

1 Comment

Awesome thanks so much. All I needed to do here is combine ${arr[2]}${arr[3]}. Great solution.
1

In bash using string manipulation:

 ~$ sel1=L50CA
 ~$ part1=$(expr match $sel1 "\([A-Z]\+\).*")
 ~$ part2=$(expr match $sel1 "[A-Z]*\([0-9]\+\).*")
 ~$ part3=$(expr match $sel1 "[A-Z]*[0-9]*\([A-Z]*\)")
 ~$ echo $part{1,2,3}
 L 50 CA
 ~$ arr=($part{1,2,3})
 ~$ echo ${arr[@]}
 L 50 CA

4 Comments

I did not even know string manipulation existed in bash...how elegant. Thanks for the great solution.
That's not bash that's expr.
@EtanReisner en.wikipedia.org/wiki/Expr shows it's incorporated into the shell as a built-in command. (Here type expr gives expr is /usr/bin/expr though.)
Evaluating expressions is a builtin. expr is not. expr existed before the shell could do that sort of thing as I understand it (which would seem to be what wikipedia was trying to state there).

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.