LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 07-15-2004, 03:53 PM   #1
SheldonPlankton
Member
 
Registered: Jun 2004
Posts: 129

Rep: Reputation: 15
Brain cramp with shell script for loops


Hi

When I do this ...
Code:
bash-2.05b$ cat files
line1
line two
line_thre
bash-2.05b$ for i in `cat files `
> do
> echo $i
> done
... I get this ...
Code:
line1
line
two
line_three
"line two" gets outputted on two lines! What I want is this ...
Code:
bash-2.05b$ cat files
line1
line two
line_three
bash-2.05b$ for i in `cat files `
> do
> echo $i
> done
line1
line two
line_three
I want "line two" to be echo-ed out on one line not two lines.

Last edited by SheldonPlankton; 07-15-2004 at 03:56 PM.
 
Old 07-15-2004, 04:37 PM   #2
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
try
Code:
while read record
do
    echo $record
done < files
 
Old 07-15-2004, 05:09 PM   #3
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Try that :
Code:
bash-2.05b$ IFS="
> "
bash-2.05b$ for i in `cat files`; do
> echo $i
> done
Line1
Line two
Line three
bash-2.05b$ unset IFS
 
Old 07-16-2004, 05:10 AM   #4
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
'for' parses using spaces or tabs as separators si it see 'line' 'space' 'two' as two instances.
 
Old 07-16-2004, 06:45 AM   #5
Bebo
Member
 
Registered: Jul 2003
Location: Göteborg
Distribution: Arch Linux (current)
Posts: 553

Rep: Reputation: 31
EDIT: Oops, sorry keefaz, didn't see your IFS

To get the for loop to use newline as the separator, set the IFS variable to newline:
Code:
IFS='
'
or
Code:
IFS=$'\n'

Last edited by Bebo; 07-16-2004 at 06:47 AM.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script inside shell script treotan Linux - General 4 02-19-2009 06:34 AM
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 02:14 AM
putty display loops on listing directories with many files, through expect script vineet.k Linux - General 5 03-13-2007 09:44 AM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM
[SHELL SCRIPT] Write at the right of the shell window Creak Linux - General 2 04-02-2004 03:00 PM

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

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