jack
jack

Reputation: 1

How to split word into two word in bash (Unix)

I currently have:

${array[2]}   =  1000($s3);

but I want to split this word into two word:

1st is '1000'

2nd is '$s3

and other one is :

50000($s4)

1st: 50000

2nd: $s4

then store them as string

How do I do that?

Upvotes: 0

Views: 293

Answers (2)

steviethecat
steviethecat

Reputation: 880

Another way to do it in bash is:

for I in '1000($s3)' '50000($s4)'
do
   a="${I%(*}"
   b="${I#*(}"
   echo "1st=${a}, 2nd=${b%)}"
done

The a="${I%(}" removes everything from and including ( to the end of the variable. b="${I#(}" removes everything until and including the (. The echo 2nd=${b%)} removes the final ).

Upvotes: 0

anubhava
anubhava

Reputation: 784998

You can use read with custom IFS:

s='1000($s3)'
IFS='()' read a b <<< "$s"

echo -e "a=<$a>\nb=<$b>"
a=<1000>
b=<$s3>

Upvotes: 2

Related Questions