Converting rows to a single row

Hi all I have a file as below :

Development
System
User
Production

i want to convert the file to below format:
"Development","System","User","Production"

Is it possible with UNIX ? if so can you please give me some direction on it ?

Thanks,
Satya

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

several ways....

one way

#   echo $(cat infile) | sed -e 's/\(.*\)/\"\1\"/' -e 's/ /\",\"/g'
"Development","System","User","Production"

another:

#  s="\"";while read t; do s=$s",\""$t\"; done < infile ; echo $s | cut -c3-
"Development","System","User","Production"

or

#  awk '{t[NR]=$0}END{for (i=2;i<NR;i++){s=s"\""t"\","};print "\""t[1]"\","s"\""t[NR]"\""}' infile 
"Development","System","User","Production"

Sure their are easier ways of doing it also :slight_smile: hope this gives you some ideas to play with though..

$
$
$ cat f5
Development
System
User
Production
$
$ perl -lne 'chomp; push @x, $_; END {print "\"".join("\",\"",@x)."\""}' f5
"Development","System","User","Production"
$
$

tyler_durden

Novice-suitable Perl approach :slight_smile:

#!/usr/local/bin/perl

my $input='inputdata';
my @arr2=();

open(INP,$input) or die "Error opening input file $input: $!\n";
 my @arr1=<INP>;
close(INP);

 foreach my $record (@arr1) {
 chomp $record;
 push @arr2, "\"$record\"";
 push @arr2, ",";           }

 pop(@arr2);

 foreach my $record (@arr2) {
 chomp $record;
 print $record;             }

 print "\n";

Hi.

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.

cheers, drl

$ sed 's/.*/"&"/' file1 | paste -sd, -
"Development","System","User","Production"

Hi, scottn.

Good work -- better than my suggestion in both commands ... cheers, drl

Hi Everybody ,

Thanks a lot for your replies , It was a mistake from my side i didn't want the " " instead of that i need ' '. i.e. the o/p file i wanted is :

'Development','System','User','Production'
I tried to tweak your commands but i am not able to get the result. Please help me .

Thanks ,
Satya

Hi.

It's not a big tweak...

$ sed "s/.*/'&'/" file1 | paste -sd, - 
'Development','System','User','Production'

Hi scottn,

Yeah you are rite i was tring to replace the " " with ' ', but it was throwing error. Anyways thanks a lot for your help.

Thanks,
Satya