[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: scripting - cat breaking line



On 6/24/07, Matus UHLAR - fantomas <uhlar@fantomas.sk> wrote:
On 23.06.07 14:23, L.V.Gandhi wrote:
> Subject: scripting - cat breaking line

> I have a file temp1 as below
> lvgandhi@lvgvaio:~/stock$ cat temp1
> ABB,ABB LTD.,      4730.00,      4779.00,       4700.00,      4726.45,59655
> ACC,ACC LIMITED,       860.00,       864.90,       844.30,       852.25
> ,228318
> When I run on command line as
> lvgandhi@lvgvaio:~/stock$ for line in $(cat temp1);do echo
> "20070622,$line">>temp2 ;done

it's not "cat" who is breaking lines. This script will fetch content of
"temp1" file to the script, and bash splits fields by any whitespace
charactes, including spaces and tabs, so the "echo" command is calles for
every "word" in a file and echo puts newline at the end of output line by
default.

Thanks for the explanation.

> I get temp2 as
> lvgandhi@lvgvaio:~/stock$ cat temp2
> 20070622,ABB,ABB

> line is breaking at every space.
> How to avoid this?

either change IFS only to contain newline, or forget using $(cat ...) and
use different cycle:

while read line
do
        echo "20070622,$line"
done < temp1 > temp2

(or >>temp2 if you want to append to the "temp2" file)

Thanks for the solution. Simpler one was given by Tong.
sed 's/^/20070622,/' temp1 >> temp2
--
L.V.Gandhi
http://lvgandhi.tripod.com/
linux user No.205042
Reply to: