Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 18th, 2005, 11:37 AM   #1
bryanlharris
Newbie
 
Join Date: Jun 2005
Location: Texas
Posts: 3
Rep Power: 0 bryanlharris is on a distinguished road
for, cat, and echo

This is something I am curious about. I tried these commands and
expected one behavior but received another. Can anyone explain
further?

Conditions:
I have a file on my computer with the following contents:
Now is the time
for all good men
to come to the aid
of their country.

Expected Behavior(this doesn't really happen, but I wish it did):
for i in `cat file_1`; do echo $i; done


Now is the time
for all good men
to come to the aid
of their country.

Actual Behavior(this is what really happens):
for i in `cat file_1'; do echo $i; done


Now
is
the
time
for
all
good
men
to
come
to
the
aid
of
their
country.

I just don't understand why it is putting each word on a different line. Can anyone acheive the expected behavior? What am I doing wrong?

-Bryan
bryanlharris is offline   Reply With Quote
Old Jun 18th, 2005, 11:51 AM   #2
bryanlharris
Newbie
 
Join Date: Jun 2005
Location: Texas
Posts: 3
Rep Power: 0 bryanlharris is on a distinguished road
By the way I posted this same thing on alt.comp.lang.shell.unix.bourne-bash about four days ago and no replies. I guess that place is dead.
bryanlharris is offline   Reply With Quote
Old Jun 18th, 2005, 11:52 AM   #3
Dameon
Troll
 
Dameon's Avatar
 
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4 Dameon is on a distinguished road
Echo by default outputs a newline character at the end of what you input. According to the man page, you can use the -n option to prevent this. Try changing your second statement to "do echo -n $i;". Note however that spaces are removed when doing a for, so you will have to re-add them.

My solution which is likely against every bash coding guideline in existance:
 for i in $(cat cat_file_1); do echo -n $i; echo -n ' '; done; echo

The last echo is to add a newline to the end.
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270

Last edited by Dameon; Jun 18th, 2005 at 11:58 AM.
Dameon is offline   Reply With Quote
Old Jun 20th, 2005, 9:26 AM   #4
jim mcnamara
Hobbyist Programmer
 
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4 jim mcnamara is on a distinguished road
for loops are not used to read whole lines of text input, normally. This is because you get wierdness with IFS when you try to set it to newline.

The reason is that for reads fileds as separated by a space rather than a whole record
like the read command does. Try playing with IFS

try this:
#! /bin/ksh
while read record
do
    echo "$record"
done < filename

IFS="\n"

for i in `cat filename`
do
    echo "$i"
done
IFS=" "
jim mcnamara is offline   Reply With Quote
Old Jun 27th, 2005, 9:40 AM   #5
bryanlharris
Newbie
 
Join Date: Jun 2005
Location: Texas
Posts: 3
Rep Power: 0 bryanlharris is on a distinguished road
Thanks jim mcnamara for the great response. I think I will start using that read statement from now on. When running the for loop with IFS="\n" I do in fact get really strange results.

#!/bin/bash

while read record
do
echo "$record"
done < file

IFS="\n"

for i in `cat file`
do
echo "$i"
done
IFS=" "

My results look really nice using read and really weird using for i in `echo file`....
now is the time for
all good men to come
to the aid of their
countrymen.

ow is the time for
all good me
 to come
to the aid of their
cou
tryme
bryanlharris is offline   Reply With Quote
Old Jun 27th, 2005, 10:53 AM   #6
ballsaq
Newbie
 
ballsaq's Avatar
 
Join Date: Mar 2005
Location: Australia
Posts: 5
Rep Power: 0 ballsaq is on a distinguished road
I might be wrong, but wouldn't you want IFS="\\n" because your looking for \n as the line terminator. And for it to read that it needs the extra \ or am I just losing my mind here?
__________________
War isn't won by the side with the most brains, rather it is lost by the side with the most dopes. :rolleyes:
ballsaq is offline   Reply With Quote
Old Jun 27th, 2005, 11:31 AM   #7
TooDice
Newbie
 
Join Date: Jun 2005
Location: UK
Posts: 14
Rep Power: 0 TooDice is on a distinguished road
Quote:
Originally Posted by ballsaq
I might be wrong, but wouldn't you want IFS="\\n" because your looking for \n as the line terminator. And for it to read that it needs the extra \ or am I just losing my mind here?
I know what you're getting at but I think you're thinking a little too hard about it. IFS=\\n would actually look for the string "\n" as you're escaping the \ character. You shouldn't be looking for that string, you need to look for a new line character. Therefore "\n" is correct. It's difficult to explain, did anyone understand that?
__________________
If it squeals, don't smoke it.
TooDice is offline   Reply With Quote
Old Jul 3rd, 2005, 9:10 AM   #8
mackenga
Professional Programmer
 
Join Date: Mar 2005
Location: Glasgow, Scotland
Posts: 314
Rep Power: 4 mackenga is on a distinguished road
Wouldn't there be an extra round of substitution anyway? This would mean using \\n was equivalent to \n, so you'd still be as well to save a byte, but I think you'd end up with a newline as the field separator either way. Keeping track of quoting and substitution is the main reason you'll rarely see me outside of tclsh. I'm too stupid for bash.
mackenga is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:11 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC