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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
11-16-2005, 09:05 PM
|
#1
|
Member
Registered: Mar 2005
Posts: 34
Rep:
|
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?
|
|
|
11-16-2005, 11:26 PM
|
#2
|
Member
Registered: Aug 2004
Location: Todd Mission Texas
Distribution: Linspire
Posts: 215
Rep:
|
cut with delimiters. or read $1 $2 $3 $4
Last edited by Dave Kelly; 11-16-2005 at 11:27 PM.
|
|
|
11-17-2005, 02:30 AM
|
#3
|
Member
Registered: Mar 2005
Posts: 34
Original Poster
Rep:
|
what command can split with deliminater?
|
|
|
11-17-2005, 03:48 AM
|
#4
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,516
|
cut -d<delimiter> -f<fieldnums>
read the manual page for full info
|
|
|
11-17-2005, 04:37 AM
|
#5
|
Member
Registered: Mar 2005
Posts: 34
Original Poster
Rep:
|
it seen like cut need file as input, can i pass the ip address string as input?
|
|
|
11-17-2005, 04:47 AM
|
#6
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,516
|
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
|
|
|
09-05-2008, 01:40 PM
|
#8
|
LQ Newbie
Registered: Sep 2005
Distribution: Debian
Posts: 2
Rep:
|
#!/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]}"
|
|
|
09-05-2008, 02:27 PM
|
#9
|
Moderator
Registered: May 2001
Posts: 29,415
|
Code:
IP=1.2.3.4; IP=(${IP//./ }); Rev=${IP[3]}.${IP[2]}.${IP[1]}.${IP[0]}
|
|
1 members found this post helpful.
|
09-05-2008, 03:20 PM
|
#10
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
Hmmm... I doubtfully think the OP could be still interested in this topic after 3 years...
|
|
|
09-05-2008, 06:08 PM
|
#11
|
LQ Newbie
Registered: Sep 2005
Distribution: Debian
Posts: 2
Rep:
|
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`.
|
|
|
09-06-2008, 11:11 AM
|
#12
|
Moderator
Registered: May 2001
Posts: 29,415
|
Quote:
Originally Posted by jimhertzler
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.
|
|
|
06-15-2011, 11:17 AM
|
#13
|
LQ Newbie
Registered: Jun 2011
Posts: 1
Rep:
|
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.
|
|
|
06-20-2011, 12:01 AM
|
#14
|
Senior Member
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
|
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 08:03 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|