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 > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to convert a single column into several rows and columns? ashton_smith UNIX for Dummies Questions & Answers 5 05-24-2008 05:44 PM
shell script required to convert rows to columns suresh3566 Shell Programming and Scripting 2 05-07-2008 06:25 AM
How to check Null values in a file column by column if columns are Not NULLs Mandab Shell Programming and Scripting 7 03-15-2008 09:57 AM
single column to multiple columns agibbs UNIX for Dummies Questions & Answers 7 12-05-2007 10:04 PM
Print last 4 columns (variable column #) Da_Duck UNIX for Dummies Questions & Answers 19 02-27-2004 10:33 AM

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 12-05-2005
NickC NickC is offline
Registered User
  
 

Join Date: Dec 2005
Posts: 3
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 like this

X1 Y1 X2 Y2 X3 Y3 X4 Y4
X5 Y5 X6 Y6 etc.

I would prefer this to be in specfic format - 8 characters per column. But if that is not possible, each field can be separated with commas.

I thought awk with a printf would be the best way to make this happen, but I can't get it to work.
  #2 (permalink)  
Old 12-05-2005
Unbeliever Unbeliever is offline
Registered User
  
 

Join Date: Jul 2005
Location: England
Posts: 183
awk '{printf("%8s%8s",$1,$2);a+=1;if ( a%4 == 0) print "";}' inputfile.txt
  #3 (permalink)  
Old 12-05-2005
NickC NickC is offline
Registered User
  
 

Join Date: Dec 2005
Posts: 3
Worked like a charm - thanks!! Could you briefly explain what the stuff following the prinf means? With printf, it seems you're printing column 1 and 2 side by side of 8 characters per column. Then it seems you're repeating this 4 times to get the next 3 sets of data. But how does it create a new line once 4 sets of data have been printed across?
  #4 (permalink)  
Old 12-05-2005
Unbeliever Unbeliever is offline
Registered User
  
 

Join Date: Jul 2005
Location: England
Posts: 183
Here is the code spread out a bit more for readability, with line numbers proceeding so I can reference.
Code:
1: printf("%8s%8s",$1,$2);
2: a+=1;
3: if ( a%4 == 0)
4:   print "";
In this case the whole piece of code is run for each line of input.

Line1:
This prints out columns 1 and 2 ($1 and $2) of the input line ... the '%8s' means they get printed out in 8 character columns left padded with spaces.

Line 2:
Once the line has been read and the first 2 columns have been printed we increment a counter by one.

Line 3:
This test to see if our counter variable (the one we just incremented) is a multiple of 4 or not. the '%' operator returns the remainder after dividing the 1st argument (a) by the second (4).

Line 4:
So if the remainder of "a/4" is zero (ie. it is divisible by 4) then we print a new line. The reason this works is that the 'print' command always prints a new line character as opposed to the printf command which doesn't. This has the effect of printing a new line every 4 lines of input.

Hope that helps ...
  #5 (permalink)  
Old 12-05-2005
NickC NickC is offline
Registered User
  
 

Join Date: Dec 2005
Posts: 3
Thanks for the clear explanation!
  #6 (permalink)  
Old 06-28-2008
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,088
Code:
cat a | paste - - - -| sed 's/ / /g'
  #7 (permalink)  
Old 12-05-2005
futurelet futurelet is offline
Registered User
  
 

Join Date: Jul 2005
Posts: 137
Quote:
Originally Posted by Unbeliever
awk '{printf("%8s%8s",$1,$2);a+=1;if ( a%4 == 0) print "";}' inputfile.txt
Why "a+=1"?
Awk already has NR.

Also, Awk isn't C. No ";" is needed after 'print ""'.

Code:
awk '{printf "%8s%8s",$1,$2;if ( NR%4 == 0) print ""}'
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 01:32 PM.


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