split one column into multiple columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting split one column into multiple columns
# 1  
Old 06-29-2009
split one column into multiple columns

hey,

i have the following data:
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
4.600000
9.800000
12.70000
12.90000
33.80000

The following command , awk '{a[NR%14+1]=a[NR%14+1]" "$0} END {for (i in a){print a[i]}}', yields:

0.000000 0.000000 0.000000
0.000000 0.000000 0.000000
0.000000 0.000000 0.000000
0.000000 0.000000 0.000000
0.000000 0.000000 0.000000
0.000000 0.000000 0.000000
0.000000 0.000000 0.000000
0.000000 0.000000 4.600000
0.000000 0.000000 9.800000
0.000000 0.000000 12.70000
0.000000 0.000000 33.80000
0.000000 0.000000 12.90000
0.000000 0.000000 0.000000
0.000000 0.000000 0.000000

where i want the last column to be

0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
4.600000
9.800000
12.70000
12.90000
33.80000. The ascending order is lost...

Can anyone help?

ThanxSmilie
# 2  
Old 06-29-2009
Code:
nawk '
   {idx=FNR%14+1} 
   !(idx in a){key[++key[0]]=idx}
   {a[idx]=a[idx] OFS $0} 
    END {for(i=1;i<=key[0];i++) print a[key[i]]}' myFile

# 3  
Old 06-29-2009
nice, that worked perfectly...
Thanx alot
# 4  
Old 06-29-2009
Quote:
Originally Posted by vgersh99
Code:
nawk '
   {idx=FNR%14+1} 
   !(idx in a){key[++key[0]]=idx}
   {a[idx]=a[idx] OFS $0} 
    END {for(i=1;i<=key[0];i++) print a[key[i]]}' myFile

you probably need to this tweak if to be a bit more pedantic:
Code:
nawk '
   {idx=FNR%14+1} 
   !(idx in a){key[++key[0]]=idx}
   {a[idx]=(idx in a) ? a[idx] OFS $0 : $0} 
    END {for(i=1;i<=key[0];i++) print a[key[i]]}' myFile

# 5  
Old 06-29-2009
cool, I'll use it and see what it does differently under different situations...
not quite sure what you did, but it works... hehe.
Can make out some of the code, but need to check out the rest unless you can explain it better to me...
Thanx alot
# 6  
Old 06-29-2009
Quote:
Originally Posted by vgersh99
you probably need to this tweak if to be a bit more pedantic:
Code:
nawk '
   # build the index (idx) for the associative array
   {idx=FNR%14+1} 

   # if index (idx) is NOT in array (a), create annay "key" 
   # the index to "key" is the incremented counter stored in key[0]
   # at the end of the processing, key[0] will contain the number of
   # UNIQUE indecies (idx)/
   !(idx in a){key[++key[0]]=idx}

   # if "idx" in already in "a", add "$0" to it; if "idx" is NOT in a assign $0 
   # to it - this prevents a[idx] have a leading field separator/OFS
   {a[idx]=(idx in a) ? a[idx] OFS $0 : $0} 

    # iterate through array "a" indexed by the values stored in array "key"
    # indexed from 1 to max stored in "key[0]"
    END {for(i=1;i<=key[0];i++) print a[key[i]]}' myFile

The above assumes that the input 'stream' has already been sorted in the desired output order.
# 7  
Old 06-30-2009
Code:
nawk -v col="$1" '{_[NR]=$0;lines=NR}
END{
tmp=lines/col
for(i=1;i<=tmp;i++)
{
for(j=1;j<=col;j++)
printf("%s ",_[i+(j-1)*col])
print ""
}
}' yourfile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Using awk to split a column into two columns

Hi, I am trying to split the following output into two columns, where each column has Source: Destination: OUTPUT TO FILTER $ tshark -r Capture_without_mtr.pcap -V | awk '/ (Source|Destination): /' | more Source: x.x.x.x Destination: x.x.x.x Source:... (2 Replies)
Discussion started by: sand1234
2 Replies

2. UNIX for Beginners Questions & Answers

Split into multiple files by using Unique columns in a UNIX file

I have requirement to split below file (sample.csv) into multiple files by using the unique columns (first 3 are unique columns) sample.csv 123|22|56789|ABCDEF|12AB34|2019-07-10|2019-07-10|443.3400|1|1 123|12|5679|BCDEFG|34CD56|2019-07-10|2019-07-10|896.7200|1|2... (3 Replies)
Discussion started by: RVSP
3 Replies

3. Shell Programming and Scripting

awk split columns to row after N number of column

I want to split this with every 5 or 50 depend on how much data the file will have. And remove the comma on the end Source file will have 001,0002,0003,004,005,0006,0007,007A,007B,007C,007E,007F,008A,008C Need Output from every 5 tab and remove the comma from end of each row ... (4 Replies)
Discussion started by: ranjancom2000
4 Replies

4. UNIX for Dummies Questions & Answers

Split 1 column into numerous columns based on patterns

Hi, I have a text file 'Item_List.txt' containing only 1 column. This column lists different products, each separated by the same generic string header "NEW PRODUCT, VERSION 1.1". After this the name of the product is given, then a delimiter string "PRODUCT FIELD", and then the name of the... (11 Replies)
Discussion started by: mmab
11 Replies

5. Shell Programming and Scripting

How to split all columns into multiple columns?

Hi, all. How can I split all columns into multiple columns separated by tab? Input: qq ee TT 12 m1 aa gg GG 34 2u zz dd hh 56 4h ww cc JJ 78 5y ss ff kk 90 j8 xx pp mm 13 p0 Output: q q e e T T 1 2 m 1 a a g g G G 3 4 2 u z z d d h h 5 6 4 h w w c c J J 7 8 5 y (8 Replies)
Discussion started by: huiyee1
8 Replies

6. Shell Programming and Scripting

awk split columns after matching on rows and summing the last column

input: chr1 1 2 3 chr1 1 2 4 chr1 2 4 5 chr2 3 6 9 chr2 3 6 10 Code: awk '{a+=$4}END{for (i in a) print i,a}' input Output: chr112 7 chr236 19 chr124 5 Desired output: chr1 1 2 7 chr2 3 6 19 chr1 2 4 5 (1 Reply)
Discussion started by: jacobs.smith
1 Replies

7. Shell Programming and Scripting

Split text separated by ; in a column into multiple columns

Hi, I need help to split a long text in a column which is separated by ; and i need to print them out in multiple columns. My input file is tab-delimited and has 11 columns as below:- aRg02004 21452 asdfwf 21452 21452 4.6e-29 5e-29 -1 3 50 ffg|GGD|9009 14101.10 High class -node. ; ffg|GGD|969... (3 Replies)
Discussion started by: redse171
3 Replies

8. UNIX for Dummies Questions & Answers

Split a column into multiple columns at certain character count

Hey everyone, I have an issue with a client that is passing me a list of values in one column, and occasionally the combination of all the values results in more than an 255 character string. My DB has a 255 character limit, so I am looking to take the column (comma delimited file), and if it... (1 Reply)
Discussion started by: perekl
1 Replies

9. UNIX for Dummies Questions & Answers

split one column into multiple columns

hey guys... Im looking to do the following: 1 2 3 4 5 6 7 8 9 Change to: 1 4 7 2 5 8 3 6 9 Did use | perl -lpe'$\=$.%3?$":"\n"' , but it doesnt give me the matrix i want. (3 Replies)
Discussion started by: zaneded
3 Replies

10. Web Development

split the fields in a column into 3 columns

Have a column "address" which is combination of city, region and postal code like. Format is : city<comma><space>region<space>postal code abc, xyz 123456 All these three city, region and postal code are not mandatory. There can be any one of the above. In that case a nell... (2 Replies)
Discussion started by: rakshit
2 Replies
Login or Register to Ask a Question