Hi! I'm doing bash shell scripting and would like to know if bash had something similar to perl's split function? Ultimately, I want to split two strings (delimeter = '.') and compare each of their values. Thus, I figured putting them in an array would be easiest.
So i.e.:
String 1: 21.14.51
String 2: 20.15.21
I want to get:
Array1: [21][14][51]
Array2: [20][15][21]
So that I can run a for-loop to compare the values. Does anyone recommend any other method for doing this?
IFS=:
echo "$string1" | read arr1[1] arr1[2] arr1[3]
echo "$string2" | read arr2[1] arr2[2] arr2[3]
IFS=" "
Or use the bash initializer
arr1=( `echo "$string1" | tr -s ':' ' '` )
it works beautifully! much thanks!
#!/bin/bash
# Split the command line argument on the colon character.
SaveIFS=$IFS
IFS=":"
declare -a Array=($*)
IFS=SaveIFS
echo "Array[0]=${Array[0]}"
echo "Array[1]=${Array[1]}"
echo "Array[2]=${Array[2]}"
echo "Array[3]=${Array[3]}"
# IFS="."
# s=21.14.51
# set -- $s
# arr=( $s )
# echo ${arr[0]} ${arr[1]} ${arr[2]}
21 14 51
Go to:
bash shell script split array - LinuxQuestions.org
And see:
IP=1.2.3.4; IP=(${IP//./ }); Rev=${IP[3]}.${IP[2]}.${IP[1]}.${IP[0]}