Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 01-20-2005, 04:24 PM   #1
Registered: Nov 2004
Location: Boston
Posts: 30

Rep: Reputation: 15
Parse String in a Bash script


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 "" 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,
Old 01-20-2005, 05:09 PM   #2
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.


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

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

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

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

Rep: Reputation: 59
And don't forget 'tr'.
You beat me to it avarus!
for i in `cat ${a} | tr '.' '\n'` ; do
   let n=$n+1
   var=`echo "var${n}"`
   echo $var is ... ${i}

Last edited by homey; 01-20-2005 at 05:50 PM.
Old 01-20-2005, 08:51 PM   #5
Senior Member
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Wheezy/Jessie/Sid, Linux Mint DE
Posts: 4,492

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

Old 01-21-2005, 09:06 AM   #6
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.

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

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

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

The results would be:

1 0 65 105

Or you could use:

echo "" | 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
LQ Newbie
Registered: Dec 2007
Posts: 13

Rep: Reputation: 1
without tr, with bash only

v=; 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: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
$ v=''

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

$ printf '%s\n' "${v[@]}"
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.


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


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
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

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

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