The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

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 01:25 PM
awk - split function fusionX Shell Programming and Scripting 7 04-10-2008 03:32 AM
split files by specifying a string (bash shell) vikas027 Shell Programming and Scripting 12 11-01-2007 01:57 PM
perl split function new2ss Shell Programming and Scripting 5 06-08-2006 11:17 PM
split function ktsirig UNIX for Dummies Questions & Answers 4 09-29-2005 11:58 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-15-2006
eur0dad eur0dad is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 8
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 06:16 PM..
  #2 (permalink)  
Old 07-15-2006
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,800

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 ':' ' '` )

  #3 (permalink)  
Old 07-16-2006
eur0dad eur0dad is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 8
it works beautifully! much thanks!
  #4 (permalink)  
Old 09-05-2008
Jim Hertzler Jim Hertzler is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 2
#!/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]}"
  #5 (permalink)  
Old 09-05-2008
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,556

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

  #6 (permalink)  
Old 09-05-2008
Jim Hertzler Jim Hertzler is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 2
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]}
Closed Thread

Bookmarks

Tags
bash, perl split, shell scripting, tokenize

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:51 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0