Visit Our UNIX and Linux User Community


How to convert a single column into several columns?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to convert a single column into several columns?
# 8  
Old 06-06-2011
Quote:
Originally Posted by tukuyomi
Using Awk
Code:
awk '{A[(NR-1)%3]=A[(NR-1)%3]$0" ";next}END{for(i in A)print A[i]}' your_infile

how does awk ensures the array's index order. Usually it has arbitrary order, an you may end up with different line order in the output.
# 9  
Old 06-06-2011
Hi getmmg,

Still does not work for my desired output. Let me clear my input and outful files as shown below...

inputfile.txt:
Code:
AAA
BBB
CCC
DDD
EEE
FFF
GGG
HHH
III
JJJ
KKK
LLL
MMM
NNN
OOO




output.txt file is like this...
Code:
AAA     FFF     KKK
BBB     GGG     LLL
CCC     HHH     MMM
DDD     III     NNN
EEE     JJJ     OOO

Thanks in advance.


Br,
Pinpe
# 10  
Old 06-06-2011
Have you tried my command,

it worked fine.
Code:
cgi@tioman> (/home/cgi) $ awk -v row=5 '{arr[NR%row]=arr[NR%row]" "$0} END{n=asort(arr);for(i=1;i<=n;i++){print arr[i]}} ' test.txt
 AAA FFF KKK
 BBB GGG LLL
 CCC HHH MMM
 DDD III NNN
 EEE JJJ OOO

# 11  
Old 06-06-2011
Hi kumaran_5555,

I'm getting error...

Code:
root@pinpe>awk -v row=5 '{arr[NR%row]=arr[NR%row]" "$0} END{n=asort(arr);for(i=1;i<=n;i++){print arr[i]}}' inputfile.txt
awk: syntax error near line 1
awk: bailing out near line 1

Thanks.

Br,
Pinpe
# 12  
Old 06-06-2011
Hi Pinpe,

if you are using solaris...please use nawk instead of awk as Kumaran has mentioned he tried in Linux.

Thanks
Sha
# 13  
Old 06-06-2011
Quote:
Originally Posted by getmmg
This should work

Code:
perl -lne 'BEGIN{$cols=3;$i=0}; $Hihash{$i}.= "$_ "; $i++; $i=0 if($i == $cols); END{print $_ for values %hash}' inp

Hi getmmg,

This one works for me but it somehow re-arranged the position of the values. My inputfile is actually a list of numbers and not letters. Can you please modify a little this code of yours? Thanks.

Code:
389    
57     
4      
29     
51     
382    
527    
10625  
87868  
18     
5      
1807367
8614   
730    
7025

The output should be like this...

Code:
389     382       5      
57      527       1807367
4       10625     8614   
29      87868     730    
51      18        7025

Thanks in advance.



Br,
Pinpe
# 14  
Old 06-06-2011
Pinpe,

This should work

Code:
 
 
perl -0ane 'BEGIN{$cols=5;$k=0}END{$total=($#F+1)/$cols;foreach(@F){$hash{$k}.= "$_\t";$k++; $k=0 if($k == $total)}; print "$_\n" for values %hash}' inp


I've got following output when the cols was given as 5 and then as 3.
Let me know if this is what you require.

Output I got

Code:
 
 
5 cols
 
AAA DDD GGG JJJ MMM
BBB EEE HHH KKK NNN
CCC FFF III LLL OOO
 
3 cols
 
AAA FFF KKK
BBB GGG LLL
CCC HHH MMM
DDD III NNN
EEE JJJ OOO


Last edited by getmmg; 06-06-2011 at 12:16 PM.. Reason: Added tab for proper alignment

Previous Thread | Next Thread
Test Your Knowledge in Computers #872
Difficulty: Medium
In computer science, self-modifying code is code that alters its own instructions while it is executing.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert row to columns start from nth column

Dear All, We have input like this: 161 57 1378 176 1392 262 1444 441 1548 538 1611 670 1684 241 57 1378 208 1393 269 1447 444 1549 538 1610 677 1700 321 ... (4 Replies)
Discussion started by: attila
4 Replies

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

3. Shell Programming and Scripting

Multiple columns to a single column

I have this input: 10 22 1 100 11 22 10 1 50 14 3 1 100 23 3 1 100 24 15 1 100 10 22 5 3 1 33.333 11 22 1 100 It has an inconsistent number of fields but the last field is determined by 100/(NF-2) using awk. I want to take this multiple columned input file and transform so that... (2 Replies)
Discussion started by: mdlloyd7
2 Replies

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

5. Shell Programming and Scripting

Convert columns to single row

Hello all I have data like 1 2 3 4 5 I wish my output would be like 1,2,3,4,5 For this i have executed 'BEGIN {FS="\n"; ORS=","} {print $0}' test and got the output as 1,2,3,4,5, I do not want to have , at the end of 5. output should be like (5 Replies)
Discussion started by: vasuarjula
5 Replies

6. Shell Programming and Scripting

how to introduce a space in a single column without distrubing the other columns

Hello Experts, I am new to this forum, I would like to do the following changes in one of the column of a txt file, which is having around 9 column. For example, column 3 is having letters like this AB11 AB12 C CA CB AC1 AC2 I would like to convert the same column as follows ... (5 Replies)
Discussion started by: Fredrick
5 Replies

7. Shell Programming and Scripting

Convert two column data into 8 columns

Apologies if this has been covered - I did search but couldn't find what I was looking for. I have a simple X-Y input file. I want to convert it from two columns into 8 columns - 4 pairs of X-Y data. So my input file looks like X1 Y1 X2 Y2 X3 Y3 X4 Y4 X5 Y5 etc And I want it to look... (8 Replies)
Discussion started by: NickC
8 Replies

8. UNIX for Dummies Questions & Answers

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... (5 Replies)
Discussion started by: ashton_smith
5 Replies

9. Shell Programming and Scripting

Single column to multiple columns in awk

Hi - I'm new to the awk programming language. I'm trying to print a single column of data to several columns, and I found an article on iTWorld.com (ITworld.com - Printing in columns). It looks like the mkCols2 script is very close to what I need to do, but it looks like the end of the code... (2 Replies)
Discussion started by: astroDave
2 Replies

10. UNIX for Dummies Questions & Answers

single column to multiple columns

Hello, I have a single column of data that I would like to cut/print (with awk or ...) into multiple columns at every empty row (or common character). Input: 5.99123 5.94693 7.21383 5.95202 0.907935 5.99149 6.08427 0.975774 6.077 Output: 5.99123 5.95202 6.08427 5.94693... (7 Replies)
Discussion started by: agibbs
7 Replies

Featured Tech Videos