![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
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 |
|
||||
|
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.. |
|
||||
|
Thanks summer_cherry, for perl & nawk code
|
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Tags |
| perl, perl shift, shift, shift perl |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|