Reversing numbers in a datafile of rows and columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reversing numbers in a datafile of rows and columns
# 15  
Old 04-16-2009
ok, this is a quite a different inversion - this is NOT what you've wanted from the start.
But that's ok - try this:

# this assumes 12 [default] columns encoded into 6 columns in the file - this is my
# test case file from the earlier post
nawk -f mat.awk myFile

# this assumes 1977 columns encoded into 6 columns in the file
nawk -v cols=1977 -f mat.awk myFile

# this assumes 15 columns encoded into 6 columns in the file - as in your previously given sample file
nawk -v cols=15 -f mat.awk myFile

mat.awk:
Code:
BEGIN {
  if (cols=="") cols=12
}
{
  for(i=1; i<=NF;i++) {
    col = ((col++)%cols)+1
    if (col==1) row++
    arr[row,col]=$i
  }
}
END {
  for(i=row; i;i--)
    for(j=1; j<=cols; j++)
      printf("%s%c", arr[i,j], !(j%cols)?ORS:OFS)
}


Last edited by vgersh99; 04-16-2009 at 12:57 PM.. Reason: slight modification - no need for the 'nf'
# 16  
Old 04-17-2009
Bug

It still didn't work quite as I wanted it to.

Anyway, I managed to get a friend to write a python code which solved the problem. If anyone is interested the code is included:
Code:
#!/usr/bin/python
length_of_a_row=1977
input_file='infile.dat'
output_file='outfile.dat'
data=open(input_file).read() # opens the file and reads it into memory
data=data.split() #turns it into one long list of single numbers
data2=[] #we'll need this as an empty list in the for-loop
for i in xrange(len(data)/length_of_a_row):
 data2.append(data[length_of_a_row*i:length_of_a_row*(i+1)])
#reshapes it in the size of the matrix
data=[] # free a bit of memory
data2.reverse() #resort with first row at bottom
#get rid of the array shape:
data=" ".join([" ".join(x) for x in data2]) # turn it into one long string
data2=[]
data=data.split() # and split it into one long list again 
#get it into output shape again:
for i in xrange(len(data)/6):
 data2.append(data[6*i:6*(i+1)])
data2.append(data[6*(len(data)/6):len(data)]) #the last line is not full length
data=[] #free some memory
#write the whole stuff out to hard disk
out=open(output_file,'w') 
out.write("\n".join([" ".join(x) for x in data2]))
out.write('\n')
out.close()

# 17  
Old 04-17-2009
given mat.txt:
Code:
01 02 03 04 05 06
07 08 09 10 11 12
13 14 15 16 17 18
19 20 21 22 23 24
25 26 27 28 29 30
31 32 33 34 35 36
37 38 39 40 41 42
43 44 45 46 47 48
49 50 51 52 53 54
55 56 57 58 59 60
61 62 63 64 65 66
67 68 69 70 71 72
73 74 75

and mat.awk:
Code:
BEGIN {
  if (cols=="") cols=12
}
{
  for(i=1; i<=NF;i++) {
    col = ((col++)%cols)+1
    if (col==1) row++
    arr[row,col]=$i
  }
}
END {
  for(i=row; i;i--)
    for(j=1; j<=cols; j++)
      printf("%s%c", arr[i,j], !(j%cols)?ORS:OFS)
}

Code:
nawk -v cols=15 -f mat.awk mat.txt

produces:
Code:
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15

Anything wrong?

Last edited by vgersh99; 04-17-2009 at 01:37 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Summing up values of rows of numbers

data file contains failed=24 error=23 error=163 failed=36 error=903 i need to get a total count of each value above. i'm looking for the most efficient method to do this as the datafile i provided is just a sample. the actual data can be several hundred thousands of lines. so from... (3 Replies)
Discussion started by: SkySmart
3 Replies

2. Shell Programming and Scripting

Compare 2 csv files by columns, then extract certain columns of matcing rows

Hi all, I'm pretty much a newbie to UNIX. I would appreciate any help with UNIX coding on comparing two large csv files (greater than 10 GB in size), and output a file with matching columns. I want to compare file1 and file2 by 'id' and 'chain' columns, then extract exact matching rows'... (5 Replies)
Discussion started by: bkane3
5 Replies

3. Shell Programming and Scripting

Adding (as in arithmetic) to numbers in columns in file, and writing new file with new numbers

Hi again. Sorry for all the questions — I've tried to do all this myself but I'm just not good enough yet, and the help I've received so far from bartus11 has been absolutely invaluable. Hopefully this will be the last bit of file manipulation I need to do. I have a file which is formatted as... (4 Replies)
Discussion started by: crunchgargoyle
4 Replies

4. Shell Programming and Scripting

Evaluate 2 columns, add sum IF two columns match on two rows

Hi all, I know this sounds suspiciously like a homework course; but, it is not. My goal is to take a file, and match my "ID" column to the "Date" column, if those conditions are true, add the total number of minutes worked and place it in this file, while not printing the original rows that I... (6 Replies)
Discussion started by: mtucker6784
6 Replies

5. Shell Programming and Scripting

Read in numbers from a datafile

Hi, I want to be able to read numbers from many files which have the same general form as follows: C3H8 4.032258004031807E-002 Phi = 1.000000E+00 Tau = 5.749E+00 sL0 = 3.805542E+01 dL0 = 1.514926E-02 Tb = 2.328291E+03 Tu = 3.450E+02 Alpha = ... (3 Replies)
Discussion started by: lost.identity
3 Replies

6. Shell Programming and Scripting

Deleting all the fields(columns) from a .csv file if all rows in that columns are blanks

Hi Friends, I have come across some files where some of the columns don not have data. Key, Data1,Data2,Data3,Data4,Data5 A,5,6,,10,, A,3,4,,3,, B,1,,4,5,, B,2,,3,4,, If we see the above data on Data5 column do not have any row got filled. So remove only that column(Here Data5) and... (4 Replies)
Discussion started by: ks_reddy
4 Replies

7. Shell Programming and Scripting

sorting the datafile in an order given in second datafile

Hi, I have two files: first input file is having 7-8 columns, and second data file is like I want to arrange my datafile1 in the order given in second data file, by comparing the seconddatafile with the second column of first file and print the entire line....also if any... (2 Replies)
Discussion started by: CAch
2 Replies

8. UNIX for Dummies Questions & Answers

Insert text in datafile with uneven columns

Dear Unix Gurus, I have a dataset consisting of a number of uneven columns. What I would like to do is fill up the missing rows with an arbitrary text of fixed value so that all columns now have an equal number of rows. for example, in the sample datafile below... 1.0 1.3 0.25 2.2 2.0... (2 Replies)
Discussion started by: tintin72
2 Replies

9. Shell Programming and Scripting

reversing the rows and coloumns ina given matrix?

hi plz can any body giv ethe code to perform transpose matrix of agiven matrix in shell scripts program????? (1 Reply)
Discussion started by: chaitunanduri
1 Replies

10. Shell Programming and Scripting

Combine a datafile with Master datafile, emergent!

Hi guys, my supervisor has asked me to solve the problem in 7 days, I've taken 3 days to think about it but couldn't figure out any idea. Please give me some thoughts with the following problem, I have index.database that has only index date: 1994 1995 1996 1997 1998 1999 I have... (6 Replies)
Discussion started by: onthetopo
6 Replies
Login or Register to Ask a Question