LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-05-2010, 05:37 PM   #1
mkono
Member
 
Registered: May 2008
Posts: 50

Rep: Reputation: 16
Bash Script


Creating a bash script where im reading the following line from a text file: "192.168.1.1:1092" I want it to read that and have it split into two variables: IP and Port, using the ":" as a separator.

It would be nice to not have two awk statements, sed or cut.
Any help would be greatly appreciated.
 
Old 10-05-2010, 05:51 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,361
Blog Entries: 55

Rep: Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547Reputation: 3547
You're invited to post any (pseudo) code you've got together with any errors to receive possibly constructive comments.
 
0 members found this post helpful.
Old 10-05-2010, 07:45 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,552

Rep: Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898
I am with unSpawn, you show us what you have and where you are getting stuck (even if that is your 2 awk / other commands) and we will help you further.
 
Old 10-05-2010, 07:54 PM   #4
janhe
Member
 
Registered: Jul 2007
Location: Belgium
Distribution: slackware64 13.1, slackware 13.1
Posts: 369

Rep: Reputation: 49
Code:
j=0
for i in $(echo "192.168.0.1:25" | tr ":" " ")
do
  out[$j]=$i
  j=$j+1
done
Now ${out[0]} contains the IP, and ${out[1]} contains the port.

Another way to go about it is:
Code:
ip=$(echo "192.168.0.1:25" | cut -d: -f1)
port=$(echo "192.168.0.1:25" | cut -d: -f2)
These are the only solutions I can come up with. I know you don't want the second, but it is IMO the best bash can do for you. If it can be done with less code, I am open to learn.

P.S. I thought your post was very clear and you showed your work by mentioning awk, sed and cut.
 
Old 10-05-2010, 09:59 PM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
buf=192.168.0.1:25
IP=${buf%%:*}
port=${buf##*:}
 
1 members found this post helpful.
Old 10-05-2010, 10:24 PM   #6
carltm
Member
 
Registered: Jan 2007
Location: Canton, MI
Distribution: CentOS, SuSE, Red Hat, Debian, etc.
Posts: 703

Rep: Reputation: 97
This can be done without the use of sed, awk or cut.
Just read up on Internal Field Separators.
 
Old 10-06-2010, 10:42 AM   #7
mkono
Member
 
Registered: May 2008
Posts: 50

Original Poster
Rep: Reputation: 16
Thanks to everyone that posted helpful solutions. It is greatly appreciated!
 
Old 10-06-2010, 10:47 AM   #8
mkono
Member
 
Registered: May 2008
Posts: 50

Original Poster
Rep: Reputation: 16
Thanks janhe. Much appreciated for the excellent help.

Quote:
Originally Posted by janhe View Post
Code:
j=0
for i in $(echo "192.168.0.1:25" | tr ":" " ")
do
  out[$j]=$i
  j=$j+1
done
Now ${out[0]} contains the IP, and ${out[1]} contains the port.

Another way to go about it is:
Code:
ip=$(echo "192.168.0.1:25" | cut -d: -f1)
port=$(echo "192.168.0.1:25" | cut -d: -f2)
These are the only solutions I can come up with. I know you don't want the second, but it is IMO the best bash can do for you. If it can be done with less code, I am open to learn.

P.S. I thought your post was very clear and you showed your work by mentioning awk, sed and cut.
 
Old 10-06-2010, 10:51 AM   #9
mkono
Member
 
Registered: May 2008
Posts: 50

Original Poster
Rep: Reputation: 16
Thanks Catkin. I'm going to give this one a try also. Thanks!

Quote:
Originally Posted by catkin View Post
buf=192.168.0.1:25
IP=${buf%%:*}
port=${buf##*:}
 
Old 10-06-2010, 11:21 AM   #10
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
# buf=192.168.0.1:25
# IFS=":"
# set -- $buf
# echo $1
192.168.0.1
# echo $2
25
 
  


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
Variables and Mkvextract in a bash script and a good resource for bash help? gohmifune Linux - General 9 04-13-2011 09:37 AM
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 05:11 AM
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 12:10 PM
Strange if statement behaviour when using bash/bash script freeindy Programming 7 08-04-2008 07:00 AM
Bash script to create bash script jag7720 Programming 10 09-10-2007 08:01 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration