![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jun 2005
Location: Texas
Posts: 3
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#2 |
|
Newbie
Join Date: Jun 2005
Location: Texas
Posts: 3
Rep Power: 0
![]() |
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.
|
|
|
|
|
|
#3 |
|
Troll
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4
![]() |
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 12:58 PM. |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4
![]() |
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=" " |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Jun 2005
Location: Texas
Posts: 3
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#6 |
|
Newbie
Join Date: Mar 2005
Location: Australia
Posts: 5
Rep Power: 0
![]() |
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: |
|
|
|
|
|
#7 | |
|
Newbie
Join Date: Jun 2005
Location: UK
Posts: 14
Rep Power: 0
![]() |
Quote:
__________________
If it squeals, don't smoke it. |
|
|
|
|
|
|
#8 |
|
Professional Programmer
Join Date: Mar 2005
Location: Glasgow, Scotland
Posts: 335
Rep Power: 4
![]() |
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.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|