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
Changing one column of delimited file column to fixed width column manneni prakash Shell Programming and Scripting 5 06-22-2009 05:27 AM
Awk multiple lines with 3rd column onto a single line? SoMoney Shell Programming and Scripting 4 12-06-2008 08:59 AM
Pivoting a Single column deepakwins UNIX for Dummies Questions & Answers 2 11-10-2008 03:38 PM
Single column to multiple columns in awk astroDave Shell Programming and Scripting 2 03-27-2008 10:00 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 Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-21-2009
nica nica is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 22
paste each 10 lines of single column to several column

Hi,

I need to paste each 10 lines of single column to several columns.
Please, can anyone tell me how to write in awk?

Input File:

22
34
36
12
17
19
15
11
89
99
56
38
29
29
49
29
67
89
76
19
22
44
12
44
66
87
45
34
67
98



Desired Output:

22 56 22
34 38 44
36 29 12
12 29 44
17 49 66
19 29 87
15 67 45
11 89 34
89 76 67
99 19 98


Thanks
  #2 (permalink)  
Old 01-21-2009
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,293
Try this:

Code:
awk '
{a[NR%10]=a[NR%10]?a[NR%10]" "$0:$0}
END{a[10]=a[0];for(i=1;i<11;i++){print a[i]}}
' file
Regards
  #3 (permalink)  
Old 01-21-2009
nica nica is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 22
Hi Franklin52,

Your code is work!

Thanks so much.....
  #4 (permalink)  
Old 01-22-2009
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,078
Hi, below should be ok for you. Can change below 3 to whatever, so that make them any number of columns.

Code:
#!/usr/bin/perl
use POSIX;
sub _toArr{
	my $file=shift;
	local $/="";
	open FH,"<$file";
	my $str=<FH>;
	my @arr=split("\n",$str);
	return \@arr;
}
sub splitArr{
	my ($ref,$num)=(@_);
	my @arr=@{$ref};
	my $loop=ceil(($#arr+1)/$num);
	#print $loop,"\n";
	for(my $i=0;$i<=$loop-1;$i++){
		for(my $j=0;$j<=$num-1;$j++){
			my $tmp=$i+$loop*$j;
			print $arr[$tmp]," ";
	}
	print "\n";
	}
}
my $ref=_toArr("a.txt");
splitArr($ref,3);
Code:
line=`cat a.txt | wc -l`
nawk -v l="$line" -v col="4" '{
	num=l/col
	if(l%col!=0)
		num=int(num)+1
	arr[NR%num]=arr[NR%num]?arr[NR%num]" "$0:$0
}
END{
	arr[num]=arr[0]
	for(i=1;i<=num;i++)
		print arr[i]
}' a.txt

Last edited by summer_cherry; 01-22-2009 at 04:14 AM..
  #5 (permalink)  
Old 01-22-2009
nica nica is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 22
Thanks summer_cherry, for perl & nawk code
Sponsored Links
Closed Thread

Bookmarks

Tags
perl, perl shift, shift, shift perl

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 09:28 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