Converting rows to column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Converting rows to column
# 1  
Old 05-03-2012
Question Converting rows to column

i have output of script as below

Code:
name,roll_no,01-05-12,02-05-12,03-05-12
sam,12,24,24,24
jon,145,24,24,22
van,29,24,22,24

i want to convert these into columns as output is not fixed please tell me how to convert 1st row in to 1st columns likewise,as many rows are there are to be converted into columns

thanks in advance

Moderator's Comments:
Mod Comment Please use [code][/code] tags instead of [quote][/quote] tags for code and samples

Last edited by Scrutinizer; 05-03-2012 at 09:33 AM.. Reason: code tags / spelling
# 2  
Old 05-03-2012
If you use an array, you might achieve what you need. You might need to use some indirect reference, but it would be important to know what your script is written in to advise on code.

Additionally, is your code always comma separated and would a comma ever appear as a value? Is the data set large? If so, then probably an awk rather than shell script would be better.



I hope that someone here can help. I have a few ideas, but before knowing the above, i don't want to leap into it.


Robin
liverpool/Blackburn
UK

Last edited by rbatte1; 05-03-2012 at 09:27 AM.. Reason: Spelling
# 3  
Old 05-03-2012
Bug

dear rbatte1,

every row is comma separated and each rows max length is 33 char(33 values)
there will be max 15 rows have be converted into columns, but every time the length of characters in rows and number of rows is variable.
every row is comma separated.
this file contains output of several small scripts,combined into one row with comma
like
Code:
echo "$a, \c" >>sample.txt

like wise
and i didn't get the phrase "need to use some indirect reference" plz elaborate.
# 4  
Old 05-03-2012
You statement of
Code:
echo "$a, \c" >>sample.txt

...would actually generate a comma&space separated file. I presume you mean:-
Code:
echo "$a,\c" >>sample.txt

followed by a plain echo at the end of the process.


Anyway, this indirect phrase I used.

If you are using ksh, then the arrays are limited to being one dimensional, i.e. you can only specify one ordinate. If you could specify two, then this would be easier. The logic would work that if you had arrays A and gave them ordinates x & y in a reference that identified a single value as A[x,y] then we could load the array by using a pair of loops like this (not runnable code)
Code:
x=1
y=1

until [ $x -ge 15 ]
do
   until [ $y -gt 15 ]
   do
      {read value into A[$x,$y]}
      {increment y}
   done
   {increment x}
done

Then you could write the data out again looping the other way around, such as:-
Code:
x=1
y=1

until [ $y -ge 15 ]
do
   until [ $x -gt 15 ]
   do
      {write value from A[$x,$y]}
      {increment x}
   done
   {increment y}
done


If your shell does not support two diminsional arrays (such as ksh need to check ksh93) we may have to be more inventive. The indirect variable would be a way of trying to use a variable as a variable name, so you make one of the ordinates part of the variable, so if the 2D array is A you would actually define a set of 1D arrays, A1, A2, A3, A4, ...... for as many values of x as you would have. You could then refer to $A3[5] as being array cell 3,5 as it were. The trick is how to get the array name as a variable. It could get messy.

It depends how one would write something like:-
Code:
echo "${A$x[$y]}"

.... which I'm not sure how to code up.

I'm not sure if this helps, but it may bring further suggestions - and I may learn something too!



Robin
Liverpool/Blackburn
UK
# 5  
Old 05-04-2012
dear rbatte1

thanks for reply,but i am a very beginner in unix.I didnt get what u have exactly advised.
consider one more case
if i will break row i.e.
Code:
name,roll_no,01-05-12,02-05-12,03-05-12

and convert this into column
and same for next rows. and paste it after this first column.
will this make my task easier.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

Converting Single Column into Multiple rows, but with strings to specific tab column

Dear fellows, I need your help. I'm trying to write a script to convert a single column into multiple rows. But it need to recognize the beginning of the string and set it to its specific Column number. Each Line (loop) begins with digit (RANGE). At this moment it's kind of working, but it... (6 Replies)
Discussion started by: AK47
6 Replies

3. UNIX for Dummies Questions & Answers

merging rows into new file based on rows and first column

I have 2 files, file01= 7 columns, row unknown (but few) file02= 7 columns, row unknown (but many) now I want to create an output with the first field that is shared in both of them and then subtract the results from the rest of the fields and print there e.g. file 01 James|0|50|25|10|50|30... (1 Reply)
Discussion started by: A-V
1 Replies

4. UNIX for Dummies Questions & Answers

Converting column to rows for every 3 lines in the column

Hi gurus! Please help me with this one. I have an file with the following contents: a b c d e f g h i j I would like to make to transform it to look like this as my output file: a,b,c d,e,f (4 Replies)
Discussion started by: kokoro
4 Replies

5. Shell Programming and Scripting

Converting entries from rows to column

Hi all, I need your help on a multiple row entry into different columns. And do the same with all the entries in file. File example (showing 2 entries only, there are many like these): >ABC * AGA-AUUCUC-CGGUUCAAUCU ||| UCUAUAACCGCGCCGAGUUAGU >ABC * AGAUAU-GCUGCAGGCUCAAUUG ||||||... (2 Replies)
Discussion started by: atulkakrana
2 Replies

6. Shell Programming and Scripting

converting rows into columns

Hi, I am trying to fetch some values from db and spooling the output to a file. when i query the db for the values, i get the values in following format. PC_1 wf_test1 Test PC_2 wf_test2 Test PC_3 wf_test3 Test But my spool file was created in following format. PC_1 wf_test1 Test... (20 Replies)
Discussion started by: svajhala
20 Replies

7. UNIX for Dummies Questions & Answers

Converting columns into rows

Is there anyway to convert columns into raws using awk? (or any other command line):eek::eek::eek::eek::eek::eek::eek::eek::eek: (1 Reply)
Discussion started by: cosmologist
1 Replies

8. UNIX for Dummies Questions & Answers

Converting rows into multiple-rows

Hi every one; I have a file with 22 rows and 13 columns which includes floating numbers. I want to parse the file so that every five columns in the row would be a new record (row). For example, the first line in the old file should be converted into three lines with first two lines contain 5... (6 Replies)
Discussion started by: PHL
6 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 to Rows in a Flat file

Hi, Request To guide me in writing a shell program for the following requirement: Example:if the Input File contains the follwing data Input File Data: 80723240029,12,323,443,88,98,7,98,67,87 80723240030,12,56,6,,,3,12,56,6,7,2,3,12,56,6,7,2,3,88,98,7,98,67,87... (5 Replies)
Discussion started by: srinikal
5 Replies
Login or Register to Ask a Question