This script assumes your shell has normal "echo" syntax which interprets \c as meaning don't output a linefeed. (i.e. not the "echo -n" variant).
We output the first name surrounded by quotes and the second and subsequent lines prefixed by a comma then surrounded by quotes. The backslash before the quotes stops the shell from interpreting them.
first_time="Y"
while read line
do
if [ "${first_time}" = "Y" ]
then
echo "\"${line}\"\c"
first_time="N"
else
echo ",\"${line}\"\c"
fi
done < filename.txt
echo "" # Terminate record
$
$
$ cat f5
Development
System
User
Production
$
$ perl -lne 'chomp; push @x, $_; END {print "\"".join("\",\"",@x)."\""}' f5
"Development","System","User","Production"
$
$
And, making some assumptions about the input file (4 lines, etc), one can use paste:
#!/usr/bin/env bash
# @(#) s1 Demonstrate rows to columns, paste.
# Infrastructure details, environment, commands for forum posts.
# Uncomment export command to run script as external user.
# export PATH="/usr/local/bin:/usr/bin:/bin"
set +o nounset
pe() { for i;do printf "%s" "$i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe ; pe "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
pe "(Versions displayed with local utility \"version\")"
c=$( ps | grep $$ | awk '{print $NF}' )
version >/dev/null 2>&1 && s=$(_eat $0 $1) || s=""
[ "$c" = "$s" ] && p="$s" || p="$c"
version >/dev/null 2>&1 && version "=o" $p printf specimen paste
set -o nounset
pe
FILE=${1-data1}
# Display sample of data file, with head & tail as a last resort.
pe " || start [ first:middle:last ]"
specimen $FILE \
|| { pe "(head/tail)"; head -n 5 $FILE; pe " ||"; tail -n 5 $FILE; }
pe " || end"
pe
pe " Results:"
sed -e 's/^/"/' -e 's/$/"/' $FILE |
paste -d"," - - - - |
tee t1
# Check results.
pe
pe " Comparison with desired results:"
if cmp expected-output.txt t1
then
pe " Passed -- files are same."
else
pe " Failed -- files differ -- details:"
diff expected-output.txt t1
fi
exit 0
producing:
% ./s1
Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution : Debian GNU/Linux 5.0
GNU bash 3.2.39
printf - is a shell builtin [bash]
specimen (local) 1.17
paste (GNU coreutils) 6.10
|| start [ first:middle:last ]
Whole: 5:0:5 of 4 lines in file "data1"
Development
System
User
Production
|| end
Results:
"Development","System","User","Production"
Comparison with desired results:
Passed -- files are same.