LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 01-20-2005, 04:24 PM   #1
jimwelc
Member
 
Registered: Nov 2004
Location: Boston
Posts: 30

Rep: Reputation: 15
Parse String in a Bash script


Hello,

I am very new to bash scripting and I am trying to break down a string into a few a few separate strings.

Basically I have a Version string I retrieve from a file( for example sake lets say it is "1.0.65.105" and I would like to break it down and store it into three separte strings/or numbers. I think I might be able to use the awk command and key off of the '.' (period) tag but I can not figure out the usage.


Any suggestions or help will be greatly appreciate.


Best Regards,
-Jim
 
Old 01-20-2005, 05:09 PM   #2
mjrich
Senior Member
 
Registered: Dec 2001
Location: New Zealand
Distribution: Debian
Posts: 1,046

Rep: Reputation: 45
Yes, you could use any of sed, awk or cut. Have a look at this and this thread for recent discussions of the same problem.

Cheers,

mj
 
Old 01-20-2005, 05:15 PM   #3
avarus
Member
 
Registered: Apr 2004
Location: Oxford, UK
Distribution: Ubuntu, Debian, various
Posts: 230
Blog Entries: 5

Rep: Reputation: 33
And don't forget 'tr'.

foo='1.2.3.4'
bar=(`echo $foo | tr '.' ' '`)
echo ${bar[1]}

TIM
 
Old 01-20-2005, 05:49 PM   #4
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
Quote:
And don't forget 'tr'.
You beat me to it avarus!
Code:
#!/bin/bash
n=0
a=/home/file.txt
for i in `cat ${a} | tr '.' '\n'` ; do
   str=${str},${i}
   let n=$n+1
   var=`echo "var${n}"`
   echo $var is ... ${i}
done

Last edited by homey; 01-20-2005 at 05:50 PM.
 
Old 01-20-2005, 08:51 PM   #5
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
If you are programming in bash, you can't do without this document:

http://www.tldp.org/LDP/abs/html/

jlinkels
 
Old 01-21-2005, 09:06 AM   #6
jimwelc
Member
 
Registered: Nov 2004
Location: Boston
Posts: 30

Original Poster
Rep: Reputation: 15
Talking RE: Parse String in a Bash Script

Thank you everyone for you help. It is all appreciated.

Cheers!
 
Old 04-01-2009, 01:48 PM   #7
trogersls
LQ Newbie
 
Registered: Apr 2009
Posts: 1

Rep: Reputation: 0
The best way to do this would be:

echo "1.0.65.105" | awk 'BEGIN {FS="."}{print $1, $2, $3, $4}'

The results would be:

1 0 65 105

Or you could use:

echo "1.0.65.105" | tr '.' ' '

The results would be:

1 0 65 105

Last edited by trogersls; 04-01-2009 at 02:21 PM. Reason: forgot something
 
Old 11-08-2012, 05:09 PM   #8
Constantine
LQ Newbie
 
Registered: Dec 2007
Posts: 13

Rep: Reputation: 1
without tr, with bash only

v=1.2.3.4; IFS='.' a=($v); echo first=${a[0]}, all: ${a[*]};
 
1 members found this post helpful.
Old 11-09-2012, 07:47 AM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Code:
$ v='1.0.65.105'

$ IFS=. read -a v <<<"$v"

$ printf '%s\n' "${v[@]}"
1
0
65
105
Doing it this way has an advantage in that the resetting of IFS only applies to the read command, and not the shell as a whole.

There are lots of string manipulations available in bash.

Edit: I just noticed this is an almost 8 year old thread.

@Constantine: Reopening old threads is generally discouraged, although its usually acceptable if you have something substantial to add to that conversation, like you did here. But even then you should at least make it clear that you have re-opened it.

Thanks!

Last edited by David the H.; 11-09-2012 at 07:53 AM.
 
  


Reply



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
escape string in bash script so it can be used in command line BuckRogers01 Linux - Software 15 08-12-2010 09:38 AM
Parse RPM version string in Bash jimwelc Linux - Newbie 1 02-28-2005 05:22 PM
bash script help to parse out text slack guy Linux - Newbie 3 12-30-2004 08:42 AM
Need help with perl/bash script to parse PicBasic file cmfarley19 Programming 13 11-18-2004 05:06 PM
Bash Script String Splitting MurrayL Linux - Newbie 1 09-21-2004 03:20 AM

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

All times are GMT -5. The time now is 10:28 AM.

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
Open Source Consulting | Domain Registration