tags:

views:

337

answers:

6

hi

trade.dat is my file which consists of lines of data.

i have to concatanate each line of that file with comma (,)

help me please

+1  A: 

try

fullline=""
for line in $(cat trade.dat)
do
         fullline="$fullline,$line"
done
And then use $fullline to show youe file concatenated

hope this'll helps ;p

Ar3s
thn q very much..................
musicking123
I'd be careful with this and huge files... you'll be filling up your memory in no time.
Joachim Sauer
that is one thing to take care of, but musicking says he wants to use the whole as a variable ...
Ar3s
+7  A: 

If you mean just add a comma to the end of each line:

sed 's/$/,/' <oldfile >newfile

If you mean join all lines together into one line, separating each with a comma:

awk '{printf "%s,",$0}' <oldfile >newfile

Or the more correct one without a trailing comma (thanks, @hacker, for pointing out the error):

awk 'BEGIN {s=""} {printf "%s%s",s,$0;s=","}' <oldfile >newfile

If you want the output of any of those in a shell variable, simply use the $() construct, such as:

str=$(awk 'BEGIN {s=""} {printf "%s%s",s,$0;s=","}' <oldfile)

I find it preferable to use $() rather than backticks since it allows me to nest commands, something backticks can't do.

paxdiablo
yes sed method is also really fancy ^^
Ar3s
Except for that (awk) would give you a trailing comma.
Michael Krelin - hacker
Thanks, @hacker, fixed that one up.
paxdiablo
ya superb solution its working, but i want to capture the output to a variable.
musicking123
thn q.......................
musicking123
Wrap the command in `$()`: `var=$(awk ... < oldfile)`
Aaron Digulla
yup its working................thnk u...........
musicking123
+1, good one, your fix is smarter than my solution.
Michael Krelin - hacker
A: 

First thing that comes into my head:

gawk -- '{ if(a) { printf ",%s",$0; } else { printf "%s",$0; a=1 } }' trade.dat

if I correctly understand what you want.

Michael Krelin - hacker
+2  A: 

Two obligatory perl versions (credit goes to William Pursell for the second one):

perl -i -p -e 'chomp($_); $_ = "$_,\n"' trade.dat

perl -i -p -e 's/$/,/' trade.dat

Note that

  • this does not make backups of the original file by default (use -i.bak for that).
  • this answer appends a comma to every line. To join all lines together into a single line, separated by commas, look at William Purcell's answer.
ire_and_curses
Equivalent: perl -ipe 's/$/,/'
William Pursell
@William Pursell: Thanks - very nice. Note that you can't combine the -e option flag in this way though; it needs its own '-'.
ire_and_curses
Not quite. You're forgetting that -i swallows the rest of its argument to use as a backup extension. The most succinct you can get is `perl -pi -e 's/$/,/'`
hobbs
A: 

Answering the question in the title, one way to get each line in a variable in a loop in BASH is to:

cat file.dat | while read line; do echo -n "$line",; done

That will leave a trailing comma, but shows how to read each line.

But clearly a sed or awk or perl solutions are the best suited to the problem described in the body of your question.

Vinko Vrsalovic
+1  A: 
perl -pe 's/\n/,/ unless eof'
William Pursell