1

I am looking for an easy way to get some numeric parameters from a string.

Consider having a string like: W400Y345655T23. From this string I need to get: W=400, Y=345655 and T=23.

The number of parameters to retrieve is always the same (3 in the example: W, Y and T). Names of parameters also remain constant. But the number length can differ for each numeric value (I can have W10Y34T334 but also W100000Y2T90). Also, parameters appear in the same order.

Using Bash, is it possible to solve this using some sort of fancy scanf or String.Format equivalent way? Thankyou

13
  • Do parameters appear in the same order? Also your W100000Y2Y90 has two Y and no T, is this correct? Commented Nov 4, 2013 at 10:34
  • Yes, same order, posting this detail... Commented Nov 4, 2013 at 10:34
  • How about @fedorqui's second question?: 'Also your W100000Y2Y90 has two Y and no T, is this correct?' Commented Nov 4, 2013 at 10:46
  • 1
    @Andry If variable change positions fedorqui'as approach fails.??? What did you try? Commented Nov 4, 2013 at 11:06
  • 1
    Well my approach was to be printed, not to be used. anubhava's solution is very good and I really like @damienfrancois 's one. All together now you have many options to work with :) Commented Nov 4, 2013 at 11:33

4 Answers 4

4

Using BASH regex:

s='W400Y345655T23'
[[ "$s" =~ ^W([0-9]*)Y([0-9]*)T([0-9]*)$ ]] && echo ${BASH_REMATCH[1]}, ${BASH_REMATCH[2]}, ${BASH_REMATCH[3]}

OR to get values into BASH variables:

read W Y T < <([[ "$s" =~ ^W([0-9]*)Y([0-9]*)T([0-9]*)$ ]] && echo ${BASH_REMATCH[1]} ${BASH_REMATCH[2]} ${BASH_REMATCH[3]})

>echo $W
400
>echo $Y
345655
>echo $T
23
Sign up to request clarification or add additional context in comments.

10 Comments

Yep, through the backdoor, but working. I like the use of =~ and BASH_REMATCH, thus ⁺¹.
@Ashish: Sure I can explain. =~ BASH operator is used for regex matching so here W400Y345655T23 is matched against regex: ^W([0-9]*)Y([0-9]*)T([0-9]*)$ Each group inside parentheses is available in a BASH array called BASH_REMATCH. This command then echoes each element of BASH_REMATCH which is read using read W Y T and values are available in BASH variables W, Y and T
here Anubhava is storing values from his command into bash variables
@anubhava : Lot of stuffs to be learned from you. Thanks and my 1 upvote already to you
@Andry: Yes < <(...) is indeed the right syntax. It is called process substitution where read is accessing command's (in parentheses) output and setting BASH variables.
|
2

I'd use an associative array:

declare -A x
eval x=($(echo "W400Y345655T23" | sed 's/\([A-Z]\)/ [\1]=/g'))

Accessing the variables then is straight forward:

echo ${x[W]}

etc.

Comments

2

You can try

$ eval $( sed 's/\([A-Z]\)/ \1=/g' <<< W400Y345655T23 )
$ echo $W $Y $T
400 345655 23

It will work with any number of distinct one-capital-letter parameter name, in any order.

1 Comment

even shorter than mine. ⁺¹ :)
1

This is a workaround using grep -Po to match numbers and chars:

$ echo "W400Y345655T23" | grep -Po '\d*'
400
345655
23

$ echo "W400Y345655T23" | grep -Po '[A-Z]*'
W
Y
T

If you save these results into a variable:

c=$(echo "W400Y345655T23" | grep -Po '[A-Z]*')
d=$(echo "W400Y345655T23" | grep -Po '[0-9]*')

Then you can print in columns like this:

$ paste -d= <(echo "$c") <(echo "$d")
W=400
Y=345655
T=23

With this solution is that you can have any group of numbers / letters, and also can be different than W, Y, T.

Comments

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.