The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-23-2008
ashton_smith ashton_smith is offline
Registered User
  
 

Join Date: May 2008
Posts: 1
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 (permalink)  
Old 05-24-2008
Franklin52 Franklin52 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,294
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 12:59 PM.. Reason: typo
  #3 (permalink)  
Old 05-24-2008
drl's Avatar
drl drl is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 704
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 (permalink)  
Old 05-24-2008
Franklin52 Franklin52 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,294
drl,

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

Regards
  #5 (permalink)  
Old 05-24-2008
drl's Avatar
drl drl is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 704
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 (permalink)  
Old 05-24-2008
Franklin52 Franklin52 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,294
The OP can give the answers if he clarifies why he needs to specify the columns and rows to convert his file.

Regards
Closed Thread

Bookmarks

Tags
solaris

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:15 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0