![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !! |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Convert two column data into 8 columns | NickC | Shell Programming and Scripting | 8 | 06-28-2008 11:19 AM |
| shell script required to convert rows to columns | suresh3566 | Shell Programming and Scripting | 2 | 05-07-2008 05:25 AM |
| Single column to multiple columns in awk | astroDave | Shell Programming and Scripting | 2 | 03-27-2008 10:00 PM |
| convert rows into column | cdfd123 | Shell Programming and Scripting | 3 | 01-11-2008 12:54 PM |
| single column to multiple columns | agibbs | UNIX for Dummies Questions & Answers | 7 | 12-05-2007 10:04 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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. |
|
||||
|
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
Regards Last edited by Franklin52; 05-24-2008 at 12:59 PM.. Reason: typo |
|
|||||
|
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 |
![]() |
| Bookmarks |
| Tags |
| solaris |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|