LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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-03-2006, 06:28 PM   #1
angel115
Member
 
Registered: Jul 2005
Location: France / Ireland
Distribution: Debian mainly, and Ubuntu
Posts: 532

Rep: Reputation: 78
For in; do script


Hello

I have an issue with the fonction for in ;do
here is my script
Code:
#!/usr/bin/bash
for name in $( cat namelist.txt ); do
   echo ${name}
done
the file namelist.txt look like this:
Code:
John O'connor
Tom Anderson
Problem:
This script give me the following output:
Code:
John
O'connor
Tom
Anderson
But i was expecting the following:
Code:
John O'connor
Tom Anderson
I want the variable name to take 1 line at each loop and not only 1 word.
any idea?

Thanks,
Angel.

Last edited by angel115; 10-04-2006 at 06:04 AM. Reason: change the title
 
Old 10-03-2006, 06:35 PM   #2
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 59
You could use the IFS . For example:
Code:
#!/bin/bash

IFS=$'\n'
for name in $( cat file.txt ); do
   echo $name
done
 
Old 10-04-2006, 03:50 AM   #3
angel115
Member
 
Registered: Jul 2005
Location: France / Ireland
Distribution: Debian mainly, and Ubuntu
Posts: 532

Original Poster
Rep: Reputation: 78
Hi Homey

Thanks for your interest to my case.

I've try your solution, and here is the result:
Code:
IFS=$'\n'
for name in $( cat file.txt ); do
   echo $name
done
Give me this
Code:
Joh
 O'co

or
Tom A
derso
At each n the script met, it take it as the end of the variable.

Note: If that can help, i'm using a bash script (#!/bin/bash)
Here is the version: GNU bash, version 3.1.17(1)-release

Last edited by angel115; 10-04-2006 at 03:54 AM.
 
Old 10-04-2006, 06:19 AM   #4
titopoquito
Senior Member
 
Registered: Jul 2004
Location: Lower Rhine region, Germany
Distribution: Slackware 14.1 (32 and 64 bit)
Posts: 1,594

Rep: Reputation: 124Reputation: 124
while read name;
do
echo ${name}
done < file.txt
 
Old 10-04-2006, 05:42 PM   #5
angel115
Member
 
Registered: Jul 2005
Location: France / Ireland
Distribution: Debian mainly, and Ubuntu
Posts: 532

Original Poster
Rep: Reputation: 78
Thumbs up

Thanks it's just work fine.

Angel115
 
  


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




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