LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   bash shell script split array (http://www.linuxquestions.org/questions/programming-9/bash-shell-script-split-array-383848/)

robertngo 11-16-2005 09:05 PM

bash shell script split array
 
Hi,

I need help in making a shell script to modify DNS record, it mostly done but it need a way to split the IP into four part so i can modify reverse zone file.

Any one here can help me with this?

Dave Kelly 11-16-2005 11:26 PM

cut with delimiters. or read $1 $2 $3 $4

robertngo 11-17-2005 02:30 AM

what command can split with deliminater?

bigearsbilly 11-17-2005 03:48 AM

cut -d<delimiter> -f<fieldnums>

read the manual page for full info

robertngo 11-17-2005 04:37 AM

it seen like cut need file as input, can i pass the ip address string as input?

bigearsbilly 11-17-2005 04:47 AM

Code:

echo 1.2.3.4 | cut -d\. -f3 
3

Code:

(IFS=.;set -- $(echo 192.168.123.444) ;echo $4 $3 $2 $1)
444 123 168 192


Dave Kelly 11-17-2005 02:07 PM

Quote:

Originally posted by robertngo
it seen like cut need file as input, can i pass the ip address string as input?
Read here: http://advbash.activeventure.net/index.html
and here: http://www.catb.org/~esr/faqs/smart-questions.html

jimhertzler 09-05-2008 01:40 PM

#!/bin/bash
# Linux Aslab 2.6.24-19-generic #1 SMP Wed Aug 20 22:56:21 UTC 2008 i686 GNU/Linux

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]}"

unSpawn 09-05-2008 02:27 PM

Code:

IP=1.2.3.4; IP=(${IP//./ }); Rev=${IP[3]}.${IP[2]}.${IP[1]}.${IP[0]}

colucix 09-05-2008 03:20 PM

Hmmm... I doubtfully think the OP could be still interested in this topic after 3 years... ;)

jimhertzler 09-05-2008 06:08 PM

colucix, this question is being asked in other forums also.

unSpawn, thank you. I understand your solution after reading
Learning the bash Shell 2nt ed. by Newham and Rosenblatt page 100.
This is the best solution I have seen anywhere.
Now that I know what to look for, I see the same StringOperation-PatternMatching in the output of `set`.

unSpawn 09-06-2008 11:11 AM

Quote:

Originally Posted by jimhertzler (Post 3270996)
colucix, this question is being asked in other forums also.

No, he means you revived a stale thread instead of creating your own thread (and maybe linking to this one for reference). And about that Colucix is right.

fkhwaja 06-15-2011 11:17 AM

Here is an alternate solution using awk.
I like this solution because you do not need to set and reset $IFS.

Code:

LINE = 193.145.255.250
declare -a LINE_TOKENS

LINE_TOKENS=(`echo $LINE | awk 'BEGIN{FS=."}{for (i=1; i<=NF; i++) print $i}'`)

echo "LINE_TOKENS[0]=$(LINE_TOKENS[0])" #193
echo "LINE_TOKENS[1]=$(LINE_TOKENS[1])" #145
echo "LINE_TOKENS[2]=$(LINE_TOKENS[2])" #255
echo "LINE_TOKENS[3]=$(LINE_TOKENS[3])" #250

FS="." is where you define your delimiter.

konsolebox 06-20-2011 12:01 AM

since this thread has been revived anyway, might as well share the best solution that i know with bash:
Code:

IP=1.2.3.4
IFS=. read -a ARRAY <<< "$IP" # one-line solution
echo "$IFS" # notice that IFS didn't change afterwards
echo "${ARRAY[@]}" # shows the results



All times are GMT -5. The time now is 12:30 PM.