Converting rows to a single row


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Converting rows to a single row
# 1  
Old 05-17-2010
Converting rows to a single row

Hi all I have a file as below :

Code:
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

Moderator's Comments:
Mod Comment Use code tags please, ty.

Last edited by zaxxon; 05-17-2010 at 10:10 AM..
# 2  
Old 05-17-2010
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.

Code:
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

# 3  
Old 05-17-2010
several ways....

one way

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

another:

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

or

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

Sure their are easier ways of doing it also :-) hope this gives you some ideas to play with though..
# 4  
Old 05-17-2010
Code:
$
$
$ cat f5
Development
System
User
Production
$
$ perl -lne 'chomp; push @x, $_; END {print "\"".join("\",\"",@x)."\""}' f5
"Development","System","User","Production"
$
$

tyler_durden
# 5  
Old 05-17-2010
Novice-suitable Perl approach Smilie
Code:
#!/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";

# 6  
Old 05-17-2010
Hi.

And, making some assumptions about the input file (4 lines, etc), one can use paste:
Code:
#!/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:
Code:
% ./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
# 7  
Old 05-17-2010
Code:
$ sed 's/.*/"&"/' file1 | paste -sd, -
"Development","System","User","Production"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Splitting single row into multiple rows based on for every 10 digits of last field of the row

Hi ALL, We have requirement in a file, i have multiple rows. Example below: Input file rows 01,1,102319,0,0,70,26,U,1,331,000000113200000011920000001212 01,1,102319,0,1,80,20,U,1,241,00000059420000006021 I need my output file should be as mentioned below. Last field should split for... (4 Replies)
Discussion started by: kotra
4 Replies

2. Shell Programming and Scripting

Converting Single Column into Multiple rows

Hi .. anyone can you help me ? i need to convert text below into multiple columns interface; GigabitEthernet0/0/0/0 description; TRUNK_PE-D2-JT2-VPN_Gi0/0/0/0_TO_ME4-A-JKT-JT_4/1/1_1G mtu 9212 negotiation auto interface; GigabitEthernet0/0/0/0.11 description; tes encapsulation;... (1 Reply)
Discussion started by: mad3linux
1 Replies

3. Shell Programming and Scripting

Converting a single row to multiple rows

Hi, I want to convert a single row values to multiple rows, but the no. of rows are not fixed. For example, I have a row as below abc-def-lmn-mno-xyz out put should be get abc get def get lmn get xyz (4 Replies)
Discussion started by: Suneel Mekala
4 Replies

4. Shell Programming and Scripting

Shell Code required -Output in Multiple Rows to be in single row separated by Commas -

Hola Greetings Experts , I have records spreaded across multiple lines. in attached log.txt i want output to be in 1 line like this below Atached as Output.txt. In brief Output related to 1 line is spreaded across multiple row I wanted it to be in 1 row . Please opem the file in notepad... (4 Replies)
Discussion started by: manishK
4 Replies

5. Shell Programming and Scripting

How to merge multiple rows into single row if first column matches ?

Hi, Can anyone suggest quick way to get desired output? Sample input file content: A 12 9 A -0.3 2.3 B 1.0 -4 C 34 1000 C -111 900 C 99 0.09 Output required: A 12 9 -0.3 2.3 B 1.0 -4 C 34 1000 -111 900 99 0.09 Thanks (3 Replies)
Discussion started by: cbm_000
3 Replies

6. Shell Programming and Scripting

Converting Multiple rows to Single Row using unix commands

Can somebody help me in solving this.. Input data is like 0 A 1 B 2 C 3 D 0 A1 1 B1 2 C1 3 D1 0 A2 1 B2 2 C2 3 D2 Output should be like A B C D A1 B1 C1 D1 A2 B2 C2 D2 (7 Replies)
Discussion started by: Mahantesh Patil
7 Replies

7. Shell Programming and Scripting

Combining multiple rows in single row based on certain condition using awk or sed

Hi, I'm using AIX(ksh shell). > cat temp.txt "a","b",0 "c",bc",0 "a1","b1",0 "cc","cb",1 "cc","b2",1 "bb","bc",2 I want the output as: "a","b","c","bc","a1","b1" "cc","cb","cc","b2" "bb","bc" I want to combine multiple lines into single line where third column is same. Is... (1 Reply)
Discussion started by: samuelray
1 Replies

8. UNIX for Advanced & Expert Users

convert rows to single row

Hi I want to convert multiple rows ro single row ,I have tried with below one but I am not getting what I am expecting.Please any idea a.txt conn1=stg conn2=dev path=\xxx\a1.txt fre=a conn1=stg conn2=dev path=\xxx\a2.txt freq=a awk '/a/{ORS=" "}{print}END{print "\n"}'... (5 Replies)
Discussion started by: akil
5 Replies

9. Shell Programming and Scripting

Converting Single Column into Multiple rows

i have single column which is starting with same string(many number of rows) i have to convert each into a single row.how can i do that? laknar std mes 23 55 laknar isd phone no address amount 99 I have to convert above like below. laknar|std|mes|23|55 laknar|isd|phone... (3 Replies)
Discussion started by: laknar
3 Replies

10. Shell Programming and Scripting

Converting Column values to comma delimted single Row

I have a requirement in which i have to read a file which has multiple columns seperated by a pipe "|" from this i have to read each column values seperately and create a comma seperated row for the column and write to another file. eg: Input file: ColA ColB 1 2 2 x 3 y... (5 Replies)
Discussion started by: nvuradi
5 Replies
Login or Register to Ask a Question