LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-28-2010, 07:24 AM   #1
czezz
Member
 
Registered: Nov 2004
Distribution: Slackware/Solaris
Posts: 924

Rep: Reputation: 43
AWK - transpose


I want to do a transpose.
On the input file I have just 1 column. What I need is to put values from that column into a rows - each 8 columnes.

What I can do wno is to put all values into a 1 row... (i need to have many rows by 8 columns)

Code:
nawk '$1=$1' RS= input.file
Here is an input.file:
Code:
10004.00,
106473388.00,
1442167619.00,
192.80,
14.71,
318145.44,
941276.69,
311.00,
10037.00,
218609.00,
232698.00,
138.33,
2.95,
104.00,
1014763.31,
7.00,
10006.00,
510197111.00,
5177395202.00,
179.16,
15.42,
189461.72,
967962.06,
341.00,
11491.00,
702082782.00,
17808287130.00,
253.96,
18.02,
162678.22,
2868964.25,
81.00,
10004.00,
106473388.00,
1442167619.00,
192.80,
14.71,
318145.44,
941276.69,
311.00,
This is what I need to get:
Code:
10004.00,106473388.00,1442167619.00,192.80,14.71,318145.44,941276.69,311.00,
10037.00,218609.00,232698.00,138.33,2.95,104.00,1014763.31,7.00,
10006.00,510197111.00,5177395202.00,179.16,15.42,189461.72,967962.06,341.00,
11491.00,702082782.00,17808287130.00,253.96,18.02,162678.22,2868964.25,81.00,
10004.00,106473388.00,1442167619.00,192.80,14.71,318145.44,941276.69,311.00,
Any suggestions how to make it ?
 
Old 12-28-2010, 07:31 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Code:
awk '{printf "%s", $0}!(NR % 8){printf "\n"}' file
 
1 members found this post helpful.
Old 12-28-2010, 08:08 AM   #3
czezz
Member
 
Registered: Nov 2004
Distribution: Slackware/Solaris
Posts: 924

Original Poster
Rep: Reputation: 43
Thank you very much!
In the mean time I tryed this:
Code:
i=0;
cat file |while read row;
   do echo -n "$row";
   i=`expr $i + 1`;
      if [ "$i" == "8" ]; then 
         echo "";i=0;
      fi;
   done
...but it works like 100 times slower than your method.
AWK seems to be really powerful tool

This is how I understand this code:

Code:
'{printf "%s", $0}! - print each string %s from each line;
(NR % 8){printf "\n"}' set the Number of Rows as 8 then put \n [ENTER]
Is this make sense ?

Last edited by czezz; 12-28-2010 at 08:25 AM.
 
Old 12-28-2010, 08:27 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Code:
awk '1;!(NR%8){print "\n"}' ORS= file
Same, same but different
 
Old 12-28-2010, 08:36 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by czezz View Post
Thank you very much!
You're welcome!
Quote:
Originally Posted by czezz View Post
Code:
'{printf "%s", $0}! - print each string %s from each line;
(NR % 8){printf "\n"}' set the Number of Rows as 8 then put \n [ENTER]
Is this make sense ?
Not really. The statement
Code:
{printf "%s", $0}
prints out the whole line (record in awk) without the newline at the end. The expression
Code:
!(NR % 8)
negates (!) the remainder of the division between the current record number (NR) and 8. Take in mind that in awk any numeric value different from zero or any string different from the null string is evaluated as true. We need to print a newline whereas the remainder is 0 (that is every 8 records) so that the expression must be evaluated as true when it is 0, as false for any other value. This is the opposite of what awk does, hence the negation.
 
1 members found this post helpful.
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] awk: how can I assign value to a shell variable inside awk? quanba Programming 6 03-23-2010 02:18 AM
transpose row to column? resolute155 Programming 3 09-07-2009 02:29 PM
awk , I need help for awk, just a display function mcandy General 1 12-15-2008 12:21 PM
Transpose info between blocks from column to lines arragement cgcamal Programming 1 11-16-2008 08:48 PM
shell command using awk fields inside awk one71 Programming 6 06-26-2008 04:11 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration