paste each 10 lines of single column to several column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting paste each 10 lines of single column to several column
# 1  
Old 01-21-2009
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  
Old 01-21-2009
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  
Old 01-21-2009
Hi Franklin52,

Your code is work!Smilie

Thanks so much.....Smilie
# 4  
Old 01-22-2009
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  
Old 01-22-2009
Thanks summer_cherry, for perl & nawk code Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to copy a column of multiple files and paste into new excel file (next to column)?

I have data of an excel files as given below, file1 org1_1 1 1 2.5 100 org1_2 1 2 5.5 98 org1_3 1 3 7.2 88 file2 org2_1 1 1 2.5 100 org2_2 1 2 5.5 56 org2_3 1 3 7.2 70 I have multiple excel files as above shown. I have to copy column 1, column 4 and paste into a new excel file as... (26 Replies)
Discussion started by: dineshkumarsrk
26 Replies

2. Shell Programming and Scripting

Bring values in the second column into single line (comma sep) for uniq value in the first column

I want to bring values in the second column into single line for uniq value in the first column. My input jvm01, Web 2.0 Feature Pack Library jvm01, IBM WebSphere JAX-RS jvm01, Custom01 Shared Library jvm02, Web 2.0 Feature Pack Library jvm02, IBM WebSphere JAX-RS jvm03, Web 2.0 Feature... (10 Replies)
Discussion started by: kchinnam
10 Replies

3. Shell Programming and Scripting

Paste 2 single column files to a single file

Hi, I have 2 csv/txt files with single columns. I am trying to merge them using paste, but its not working.. output3.csv: flowerbomb everlon-jewelry sofft steve-madden dolce-gabbana-watchoutput2.csv: http://www1.abc.com/cms/slp/2/Flowerbomb http://www1.abc.com/cms/slp/2/Everlon-Jewelry... (5 Replies)
Discussion started by: ajayakunuri
5 Replies

4. Shell Programming and Scripting

Search Pattern and Print lines in Single Column

Hi Experts I have small query where I request the into a single file Suppose: File1: {Unique entries} AA BB CC DD FileB: AA, 123 AA, 234 AA, 2345 CC, 123 CC, 5678 DD,123 BB, 7890 (5 Replies)
Discussion started by: navkanwal
5 Replies

5. Shell Programming and Scripting

Problems with awk (fatal error) and paste (two variables into one column-by-column)

Hello, I have a script extracting columns of useful numbers from a data file, and manipulating the numbers with awk commands. I have problems with my script... 1. There are two lines assigning numbers to $BaseForAveraging. If I use the commented line (the first one) and let the second one... (9 Replies)
Discussion started by: vgbraymond
9 Replies

6. UNIX for Dummies Questions & Answers

Paste column from one file as column of

Any shortcuts for doing this? I need to cut the column 4 values from File1 and paste them as column4 values of File2, but only for the (first) same number of lines as File1 . All rows in File1 are contained in File2 in the exact same order, so the cut paste should work. File1 (with header and 3... (4 Replies)
Discussion started by: senhia83
4 Replies

7. Shell Programming and Scripting

Converting Single Column into Multiple rows, but with strings to specific tab column

Dear fellows, I need your help. I'm trying to write a script to convert a single column into multiple rows. But it need to recognize the beginning of the string and set it to its specific Column number. Each Line (loop) begins with digit (RANGE). At this moment it's kind of working, but it... (6 Replies)
Discussion started by: AK47
6 Replies

8. Shell Programming and Scripting

Multiple lines in a single column to be merged as a single line for a record

Hi, I have a requirement with, No~Dt~Notes 1~2011/08/1~"aaa bbb ccc ddd eee fff ggg hhh" Single column alone got splitted into multiple lines. I require the output as No~Dt~Notes 1~2011/08/1~"aaa<>bbb<>ccc<>ddd<>eee<>fff<>ggg<>hhh" mean to say those new lines to be... (1 Reply)
Discussion started by: Bhuvaneswari
1 Replies

9. Shell Programming and Scripting

How to subtract the adjacent lines from a single column?

Hi All, I have a file with only one column and i need to subtract the adjacent lines of the same column and print it in the same column. For Example: (Input) Col1 5 10 12 6 9 12 5 . . . .output should be like this: (12 Replies)
Discussion started by: Fredrick
12 Replies

10. Shell Programming and Scripting

Awk multiple lines with 3rd column onto a single line?

I have a H U G E file with over 1million entries in it. Looks something like this: USER0001|DEVICE001|VAR1 USER0001|DEVICE001|VAR2 USER0001|DEVICE001|VAR3 USER0001|DEVICE001|VAR4 USER0001|DEVICE001|VAR5 USER0001|DEVICE001|VAR6 USER0001|DEVICE002|VAR1 USER0001|DEVICE002|VAR2... (4 Replies)
Discussion started by: SoMoney
4 Replies
Login or Register to Ask a Question