How to convert a single column into several rows and columns?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to convert a single column into several rows and columns?
# 1  
Old 05-23-2008
How to convert a single column into several rows and columns?

I have a program which gives me the output as a single column with hundreds of rows like:


213
314
324
324
123

I want to be able to create a new file from this file which allows me to set the number of rows and columns in the new file, i.e. for this example, if I specify 3 rows and 2 columns, the output should look like

213 314
324 324
123


Is it possible to do this in Unix or using a pearl script?


Thank You,
Ashton.
# 2  
Old 05-24-2008
This should give the desired output:

Code:
#!/bin/sh

rows=3
cols=2

awk -v r=$rows -v c=$cols '
NR<r*c{printf("%s",NR%c?$0" ":$0"\n");next}{print}
END{if(NR%c&&NR<r*c){print ""}}' file

If you get errors use nawk or /usr/xpg4/bin/awk on Solaris.

Regards

Last edited by Franklin52; 05-24-2008 at 01:59 PM.. Reason: typo
# 3  
Old 05-24-2008
Hi.

See elsewhere on this site (the scripting forum) for a solution using paste.

You can also use pr with a few options:
Code:
#!/bin/bash -

# @(#) s2       Demonstrate column formatting.

echo
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version =o $(_eat $0 $1) pr
echo

FILE=${1-data1}

pr -a -t -s" " -2 $FILE

exit 0

producing:
Code:
$ ./s2

(Versions displayed with local utility "version")
SunOS 5.10
GNU bash 3.00.16
pr - no version provided for /usr/xpg4/bin/pr.

213 314
324 324
123

See man pages for details ... cheers, drl
# 4  
Old 05-24-2008
drl,

If I understand the question, the OP should be able to specify the number of lines and columns.

Regards
# 5  
Old 05-24-2008
Hi, Franklin52.

I allowed the data to determine the number of lines for a given number of columns -- does a general solution exist that allows both to be specified? I can't think of one off the top of my head. What should a solution do for data that exceeds rows X columns items of data?

If a specific number of lines needs to be specified with pr, paste, etc., say in the case of a limitation, then another tool could be used for that, either before or after formatting into columns... cheers, drl
# 6  
Old 05-24-2008
The OP can give the answers if he clarifies why he needs to specify the columns and rows to convert his file.

Regards
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert single column into multiple rows

Convert Single column to multiple rows file a.txt contains data like below Server=abc Run=1 Tables=10 Sessions=16 Time=380 Jobs=5 Server=abc Run=2 Tables=15 Sessions=16 Time=400 Jobs=5 Server=abc Run=3 Tables=20 Sessions=16 Time=450 (5 Replies)
Discussion started by: sol_nov
5 Replies

2. Shell Programming and Scripting

Convert rows to columns

I am looking to print the data in columns and after every 3 words it should be a new row. cat example.out | awk 'END { for (i = 0; ++i < m;) print _;print _ }{ _ = _ x ? _ OFS $1 : $1}' m=1| grep -i INNER I am looking to print in a new line after every 3 words. ... (2 Replies)
Discussion started by: lazydev
2 Replies

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

4. Shell Programming and Scripting

How to Convert rows in to columns?

Hi Gurus, How to convert rows in to columns using linux shell scripting Input is like (sample.txt) ABC DEF GHI JKL MNO PQR STU VWX YZA BCD output should be (sampleoutput.csv) ABC,DEF,GHI,JKL,MNO PQR,STU,VWX,YZA,BCD (2 Replies)
Discussion started by: infasriniit
2 Replies

5. Shell Programming and Scripting

convert rows to columns

hi, i have the file as below: abc def ghi jkl i want the output as abc,def,ghi,jki please reply, Thanks (4 Replies)
Discussion started by: namitai
4 Replies

6. Shell Programming and Scripting

How to convert a single column into several columns?

Hi I have a ksh script which gives me the output as a single column with several rows like: AAA BBB CCC DDD EEE FFF GGG HHH III I want to be able to create a new file from this file which allows me to set the number of rows and columns in the new file, i.e. for this example, if I... (30 Replies)
Discussion started by: pinpe
30 Replies

7. Shell Programming and Scripting

convert columns into rows with respect to first column

Hello All, Please help me with this file. My input file (Tab separated) is like: Abc-01 pc1 -0.69 Abc-01 E2cR 0.459666666666667 Abc-01 5ez.2 1.2265625 Xyz-01 pc1 -0.153 Xyz-01 E2cR 1.7358 Xyz-01 5ez.2 2.0254 Ced-02 pc1 -0.5714 Ced-02 ... (7 Replies)
Discussion started by: mira
7 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

convert rows into columns

Hi guys Could anyone advise me how to convert my rows into columns from a file My file would be similar to this: A11 A12 A13 A14 A15 ... A1n A21 A22 A23 A31 A41 A51 ... Am1 Am2 Am3 Am4 Am5 ... Amn The number of rows is not the same to the number of columns Thanks in advance (2 Replies)
Discussion started by: loperam
2 Replies

10. Shell Programming and Scripting

how to convert columns to rows

Hi, I need a shell script for below requirement Input file P1 - 173310 P2 - 173476 P3 - 173230 P4 - 172737 P1 - 173546 P2 - 173765 P3 - 173876 P4 - 172989 Out put file P1 173310 173546 P2 173476 173765 P3 173230 173876 P4 172737 172989 Suresh (6 Replies)
Discussion started by: suresh3566
6 Replies
Login or Register to Ask a Question

Featured Tech Videos