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 12-14-2007, 11:07 AM   #1
klavuzkarga
LQ Newbie
 
Registered: Dec 2007
Posts: 11

Rep: Reputation: 0
bash - getting tokens from string


i'm trying to separate words in a string.
and i wrote this code by referring to similar codes in the forum.
but it only gives output "aaaaaa".
i want to get all words.
what's the problem in code? what should i do?

Code:
#!/bin/bash

metin="aaaaaa bbbbbbb ccccccc dddddd"

bar=(`echo $metin | tr '.' ' '`)
n=0
for i in ${bar}
do
	echo ${bar[n]}
	let n=$n+1;
done
 
Old 12-14-2007, 11:37 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Make array of "foo" called "bar", loop through total number of elements in array bar.
Code:
foo="aaaaaa bbbbbbb ccccccc dddddd"; bar=(${foo})
for i in `seq 0 $[${#bar[@]}-1]`; do echo ${bar[$i]}; done

Last edited by unSpawn; 12-14-2007 at 11:39 AM.
 
Old 12-14-2007, 12:33 PM   #3
klavuzkarga
LQ Newbie
 
Registered: Dec 2007
Posts: 11

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by unSpawn View Post
Make array of "foo" called "bar", loop through total number of elements in array bar.
Code:
foo="aaaaaa bbbbbbb ccccccc dddddd"; bar=(${foo})
for i in `seq 0 $[${#bar[@]}-1]`; do echo ${bar[$i]}; done
as you said, i wrote this one;
Code:
#!/bin/bash

metin="aaaaaa bbbbbbb ccccccc dddddd"; bar=(${metin})

for i in `seq 0 $[${#bar[@]}-1]`;
do
	echo ${bar[$i]}
done
but i'm still getting this output;

Code:
[cumhur@localhost code]$ ./deneme.sh
aaaaaa
[cumhur@localhost code]$
 
Old 12-14-2007, 12:49 PM   #4
klavuzkarga
LQ Newbie
 
Registered: Dec 2007
Posts: 11

Original Poster
Rep: Reputation: 0
i quess this must work;
but it doesn't;
Code:
#!/bin/bash

metin="aaaaa bbbbbb ccccc dddd"

for i in $metin
do
	echo $i
done
it writes just "aaaaaa"

Last edited by klavuzkarga; 12-14-2007 at 12:50 PM.
 
Old 12-14-2007, 01:06 PM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
The code posted from unSpawn works for me, but depending on the bash version the $[ ... ] construct may or may not work. You can try to substitute with the $(( ... )) construct, as in
Code:
#!/bin/bash

metin="aaaaaa bbbbbbb ccccccc dddddd"; bar=(${metin})

for i in `seq 0 $((${#bar[@]}-1))`;
do
	echo ${bar[$i]}
done
As for your last example (indeed I don't see any reason to introduce arrays), it should be
Code:
#!/bin/bash

metin="aaaaaa bbbbbbb ccccccc dddddd"

for i in $(echo $metin)
do
   echo $i
done
 
Old 12-14-2007, 01:11 PM   #6
klavuzkarga
LQ Newbie
 
Registered: Dec 2007
Posts: 11

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by colucix View Post
The code posted from unSpawn works for me,
it's strange because i restarted my pc and tried the code,
and now, it works..
thanks a lot
 
Old 12-14-2007, 01:13 PM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
A reboot can fix a lot of things...
 
  


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
string manipulation in BASH ovince Programming 4 04-16-2007 07:15 PM
bash string processing powah Programming 3 03-14-2007 06:40 AM
bash string comparison noir911 Programming 1 01-25-2006 06:37 PM
String in Bash Shell nguyenzoro Linux - Newbie 4 11-19-2005 04:49 PM
Bash way to tell if String is in String tongar Programming 3 06-16-2005 06:59 AM

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

All times are GMT -5. The time now is 04:43 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
Open Source Consulting | Domain Registration