The UNIX and Linux Forums  


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
Script for data formatting shekhar_v4 Shell Programming and Scripting 6 01-27-2009 01:34 AM
Re-formatting of data display Raynon Shell Programming and Scripting 6 11-12-2008 10:50 PM
Formatting Data nmeliasp UNIX for Dummies Questions & Answers 3 07-24-2008 08:53 PM
Formatting info into columns straight_edge Shell Programming and Scripting 8 02-05-2007 11:51 AM
Text formatting to 132 columns jmossman UNIX for Dummies Questions & Answers 16 05-06-2002 09:34 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-09-2009
nistleloy nistleloy is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 14
Formatting a line of data into columns

Hi all,

I'm a little stuck with a data file I've been collecting data in. The file contains one field of data running continuously down the file and I can't work out how to format the data into three columns.

This is a mock up of the file: Each r# is a random number and varies in length, this goes down the file for a long ways.

Code:
r1
r2
r3
r1
r2
r3
r1
r2
r3

This is how i'd like it to look with three columns of data.

Code:
r1         r2          r3
r1         r2          r3
r1         r2          r3


Also with awk I can get data based on a field number ($1, $2) is there anyway of selecting data based on a line number?

Thanks for any responses,
nistleloy
  #2 (permalink)  
Old 05-10-2009
reborg's Avatar
reborg reborg is offline Forum Staff  
Administrator
  
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,245

Code:
awk '{ORS = (NR %3 == 0 )? "\n" : "\t" } 1' file


Code:
awk '{printf (NR %3 == 0 )? $0"\n" : $0"\t" }' file

Assuming your records are bases on lines NR is the line number.
  #3 (permalink)  
Old 05-10-2009
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,557

Code:
awk 'NR==1{s=$1;printf "%s",$1;next}
$1!=s{ printf "\t%s",$1}
$1==s{ print "";printf "%s",$1}' file

  #4 (permalink)  
Old 05-12-2009
nistleloy nistleloy is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 14
Thanks for your responses - that's help immensely.

/nistleloy

Last edited by nistleloy; 05-12-2009 at 05:52 PM.. Reason: Spelling.
Closed Thread

Bookmarks

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 06:21 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