Convert two column data into 8 columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Convert two column data into 8 columns
# 1  
Old 12-05-2005
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  
Old 12-05-2005
awk '{printf("%8s%8s",$1,$2);a+=1;if ( a%4 == 0) print "";}' inputfile.txt
# 3  
Old 12-05-2005
Worked like a charm - thanks!! Smilie 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  
Old 12-05-2005
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  
Old 12-05-2005
Thanks for the clear explanation!
# 6  
Old 12-05-2005
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 ""}'

# 7  
Old 12-06-2005
I normally do stuff in perl not awk ... so I always forget about awk's built in variables :-(

And yes ... awk is not C ... the final command termination is implicit. It doesn't harm to put it in though :-) You also avoid questions like: You terminated all your commands with ';' except the last one ... why?

I replied with an awk script since that was what the original poster has tried.
Login or Register to Ask a Question

Previous Thread | Next Thread

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. UNIX for Dummies Questions & Answers

Convert nxm Matrix into columns of data

Dear Unixers, I'm having some difficulty in converting an n x m data matrix into a dataset of 3 columns and nxm rows. As an example I want to convert this dataset 2 3 4 5 2 0.0 0.0 0.1 0.1 6 -0.3 2.0 0.0 0.3 7 -0.6 -1.1 0.5 0.3 9 -0.9 -4.1 -0.7 0.5 ... (2 Replies)
Discussion started by: tintin72
2 Replies

3. UNIX for Advanced & Expert Users

Convert column data to row data using shell script

Hi, I want to convert a 3-column data to 3-row data using shell script. Any suggestion in this regard is highly appreciated. Thanks. (4 Replies)
Discussion started by: sktkpl
4 Replies

4. Shell Programming and Scripting

How to convert a single column into several columns?

Hi I have a ksh script which gives me the output as a single column with several rows like: AAA BBB CCC DDD EEE FFF GGG HHH III 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... (30 Replies)
Discussion started by: pinpe
30 Replies

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

6. UNIX for Dummies Questions & Answers

Suggestion to convert data in rows to data in columns

Hello everyone! I have a huge dataset looking like this: nameX nameX 0 1 2 2 2 2 2 2 2 2 2 2 2 2 1 2 2 2 1 2 2 2 ............... nameY nameY 2 2 2 2 2 2 2 2 2 2 1 2 2 2 1 2 2 2 ..... nameB nameB 0 1 2 2 2 2 2 2 2 2 1 2 2 2 1 2 2 2 ..... (can be several thousands of codes) and I need... (8 Replies)
Discussion started by: kush
8 Replies

7. UNIX for Dummies Questions & Answers

Convert Rows to Columns Specific Data

I have this data M36 AREA INFORMATION MDN = 0485009346 ESN = H'15fda0b0 TYPE = HLR RESULT = NOK REASON = UNRECOGNIZED MIN COMPLETED AREA INFORMATION MDN = 0498044402 ESN = H'15fdac11 TYPE... (2 Replies)
Discussion started by: krabu
2 Replies

8. Shell Programming and Scripting

How to convert 2 column data into multiple columns based on a keyword in a row??

Hi Friends I have the following input data in 2 columns. SNo 1 I1 Value I2 Value I3 Value SNo 2 I4 Value I5 Value I6 Value I7 Value SNo 3 I8 Value I9 Value ............... ................ SNo N (1 Reply)
Discussion started by: ks_reddy
1 Replies

9. Shell Programming and Scripting

Convert row data to column data

Hi Guys, I have a file as follows: a 1 b 786 c 90709 d 99 a 9875 b 989 c 887 d 111 I want: a 1 9875 b 786 989 (3 Replies)
Discussion started by: npatwardhan
3 Replies

10. 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
Login or Register to Ask a Question