LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 10-05-2009, 01:45 AM   #1
Jeroen1000
Member
 
Registered: Sep 2009
Posts: 44

Rep: Reputation: 15
split string on file delimeter


I gave it a shot but something still isn't entirely correct

Code:
# Next split each record of data to obtain the ip address.
old_IFS=$IFS
IFS=$':'
ip="a:b:c"
for s in $ip; 

do echo $s; done
I only want the letter "a" in variable s (no spaces and no carriage return either). But when I echo s it displays:

a
b
c

A little help will surely get me there.
 
Old 10-05-2009, 02:01 AM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
You can use cut, then you don't have to muck about with the IFS
eg.

Code:
# Next split each record of data to obtain the ip address.
ip="a:b:c"
for s in $ip; 
do
echo $s | cut -d: -f1
done
cheers
 
Old 10-05-2009, 02:02 AM   #3
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by Jeroen1000 View Post
I gave it a shot but something still isn't entirely correct

Code:
# Next split each record of data to obtain the ip address.
old_IFS=$IFS
IFS=$':'
ip="a:b:c"
for s in $ip; 

do echo $s; done
I only want the letter "a" in variable s (no spaces and no carriage return either). But when I echo s it displays:

a
b
c

A little help will surely get me there.
1. Don't reset IFS. That's not necessary in modern times.

Code:
$ ip="a:b:c"
$ echo ${ip%%:*}
a
But it would help if you would say what you want, not what you don't want.
 
Old 10-05-2009, 02:25 AM   #4
Jeroen1000
Member
 
Registered: Sep 2009
Posts: 44

Original Poster
Rep: Reputation: 15
both methods work perfectly guys

Lutusp, incidentally, I was just mocking about with the method you use.

Bash is really chinese to me compared to any other language.

Cheers,

Jeroen
 
Old 10-05-2009, 03:05 AM   #5
Jeroen1000
Member
 
Registered: Sep 2009
Posts: 44

Original Poster
Rep: Reputation: 15
Dumb ass follow up question

When I use lutusp's method I can do

Code:
var=${ip%%:*}
echo $var
But because I want to learn bash, I also decided to try the other method:

The code below is not allowed
Code:
var=echo $s | cut -d: -f1
echo $var

I've tried various things, like adding () around the echo statement etc. Can someone instruct me why this assignment is not allowed?
 
Old 10-05-2009, 03:28 AM   #6
Jeroen1000
Member
 
Registered: Sep 2009
Posts: 44

Original Poster
Rep: Reputation: 15
I found the answer, don't know why it has to be this way but this is the solution:
Code:
user=`echo ${data[0]} | cut -d: -f2`
 
Old 10-05-2009, 03:30 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by Jeroen1000 View Post
Code:
var=echo $s | cut -d: -f1
echo $var
I've tried various things, like adding () around the echo statement etc. Can someone instruct me why this assignment is not allowed?
The line tells the shell "assign echo to variable var for the duration of running the following command which is ... run the command which is the value of $s and pipe its stdout to the stdin of the cut command.

What you want to do is assign the output from "echo $s | cut -d: -f1" to variable var. This will do it
Code:
var="$(echo $s | cut -d: -f1 )"
The $( ) makes the stdout from the contained line available as a string. The " " around it makes it a single string and prevents it being split at the first whitespace character and so ensures the whole of the command output is assigned to $var.
 
Old 10-05-2009, 08:35 AM   #8
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
To avoid the carriage return use 'echo -n ....'
 
  


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
[perl]How to treat string like "a b" as a single string when split? john.daker Programming 21 06-01-2009 05:57 PM
how do I split large file by string? khairil Programming 5 04-28-2008 10:37 PM
string split ovince Programming 4 06-10-2007 05:45 PM
split string prob izza_azhar Programming 3 02-08-2005 12:11 AM
split string izza_azhar Programming 6 01-18-2005 08:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:24 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