Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Search Forums:



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-15-2006
Registered User
 

Join Date: Jul 2006
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
perl split function for bash?

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?

Last edited by eur0dad; 07-15-2006 at 05:16 PM..
Sponsored Links
    #2  
Old 07-15-2006
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 8,506
Thanks: 67
Thanked 401 Times in 390 Posts

Code:
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

Code:
arr1=( `echo "$string1" | tr -s ':' ' '` )

Sponsored Links
    #3  
Old 07-16-2006
Registered User
 

Join Date: Jul 2006
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
it works beautifully! much thanks!
    #4  
Old 09-05-2008
Registered User
 

Join Date: Sep 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
#!/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]}"
Sponsored Links
    #5  
Old 09-05-2008
Registered User
 

Join Date: Sep 2006
Posts: 2,651
Thanks: 0
Thanked 14 Times in 14 Posts

Code:
# IFS="."
# s=21.14.51
# set -- $s
# arr=( $s )
# echo ${arr[0]} ${arr[1]} ${arr[2]}
21 14 51

Sponsored Links
    #6  
Old 09-05-2008
Registered User
 

Join Date: Sep 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
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]}
Sponsored Links
Closed Thread

Tags
bash, perl split, shell scripting, tokenize

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
passing variable from bash to perl from bash script arsidh Shell Programming and Scripting 10 06-04-2008 12:25 PM
awk - split function fusionX Shell Programming and Scripting 7 04-10-2008 02:32 AM
split files by specifying a string (bash shell) vikas027 Shell Programming and Scripting 12 11-01-2007 12:57 PM
perl split function new2ss Shell Programming and Scripting 5 06-08-2006 10:17 PM
split function ktsirig UNIX for Dummies Questions & Answers 4 09-29-2005 10:58 AM



All times are GMT -4. The time now is 05:05 AM.