LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-16-2005, 08:05 PM   #1
robertngo
Member
 
Registered: Mar 2005
Posts: 34

Rep: Reputation: 15
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?
 
Old 11-16-2005, 10:26 PM   #2
Dave Kelly
Member
 
Registered: Aug 2004
Location: Todd Mission Texas
Distribution: Linspire
Posts: 213

Rep: Reputation: 31
cut with delimiters. or read $1 $2 $3 $4

Last edited by Dave Kelly; 11-16-2005 at 10:27 PM.
 
Old 11-17-2005, 01:30 AM   #3
robertngo
Member
 
Registered: Mar 2005
Posts: 34

Original Poster
Rep: Reputation: 15
what command can split with deliminater?
 
Old 11-17-2005, 02:48 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,288

Rep: Reputation: 174Reputation: 174
cut -d<delimiter> -f<fieldnums>

read the manual page for full info
 
Old 11-17-2005, 03:37 AM   #5
robertngo
Member
 
Registered: Mar 2005
Posts: 34

Original Poster
Rep: Reputation: 15
it seen like cut need file as input, can i pass the ip address string as input?
 
Old 11-17-2005, 03:47 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,288

Rep: Reputation: 174Reputation: 174
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
 
Old 11-17-2005, 01:07 PM   #7
Dave Kelly
Member
 
Registered: Aug 2004
Location: Todd Mission Texas
Distribution: Linspire
Posts: 213

Rep: Reputation: 31
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
 
Old 09-05-2008, 12:40 PM   #8
jimhertzler
LQ Newbie
 
Registered: Sep 2005
Distribution: Debian
Posts: 2

Rep: Reputation: 0
#!/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]}"
 
Old 09-05-2008, 01:27 PM   #9
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,454
Blog Entries: 54

Rep: Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896
Code:
IP=1.2.3.4; IP=(${IP//./ }); Rev=${IP[3]}.${IP[2]}.${IP[1]}.${IP[0]}
 
1 members found this post helpful.
Old 09-05-2008, 02:20 PM   #10
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,508

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Hmmm... I doubtfully think the OP could be still interested in this topic after 3 years...
 
Old 09-05-2008, 05:08 PM   #11
jimhertzler
LQ Newbie
 
Registered: Sep 2005
Distribution: Debian
Posts: 2

Rep: Reputation: 0
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`.
 
Old 09-06-2008, 10:11 AM   #12
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,454
Blog Entries: 54

Rep: Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896Reputation: 2896
Quote:
Originally Posted by jimhertzler View Post
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.
 
Old 06-15-2011, 10:17 AM   #13
fkhwaja
LQ Newbie
 
Registered: Jun 2011
Posts: 1

Rep: Reputation: Disabled
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.
 
Old 06-19-2011, 11:01 PM   #14
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Split function (bash script) NiM Programming 11 03-10-2012 06:53 AM
Bash Script Array index value Kedelfor Programming 10 04-29-2009 04:37 AM
Bash script text line into an array toolshed Programming 1 06-13-2005 05:49 PM
MAJOR problem ... bash script array HELP !!!!! michael_util Slackware 1 02-13-2004 06:51 AM
shell script array problem rche3252 Programming 1 10-08-2003 11:43 PM


All times are GMT -5. The time now is 07:36 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration