PDA

View Full Version : Shell script file manipulation help...



Dman33
09-29-2004, 02:13 PM
Okay, I have not written a shell script in like 8 years, so I am trying to recall how to complete a simple task. I need to write a script that will have two arguments, file1 and file2. Both files are simple text files. We will assume that I already set up the argument error-checking. I need to open file1 and read each line in it, writing each line to file2 substituting all carriage returns with commas.

For example, file1 is:
red
yellow
blue

I want to create file2 which will be:
red,yellow,blue

There might be a simple 2-liner which will involve using awk but I cannot come up with how. Now, I can come up with some clumsy ways to do this in C++ or something, and I could whip this up in 2 minutes using VBScript or KixStart but I need it in shell script. Any ideas?

Meanwhile I will be dusting my old books from college off... :dead:

Cubsfan
09-29-2004, 02:19 PM
Try:



#!/bin/sh


OUTPUT=
while read LINE
do

OUTPUT="$OUTPUT,$LINE"

done < $1

echo $OUTPUT > $2


Something like that. Didn't test it out so I'm not sure. There's also an argument to echo (differs depending on the OS) that will print it out to file wihtout adding a \n, so you could replace OUTPUT= line with something like
echo -c ",$LINE" >> $2

Something like that