Dividing all columns by the nth row


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Dividing all columns by the nth row
# 1  
Old 10-20-2011
Dividing all columns by the nth row

Hi All!

if I have a file like this:


Code:
8  10  12  14  16  18  0  2  6
2   4    6    8    10  12  16  18  10  
6  8  12  16  18  0  2  2  6
2  2  2  2  2  2  2  2  2   
10  12  16  4  8  16  4  16  0
 8  10  14  16  0  4  8  12  14

And I want to divide all the lines by line 4 for example (except column 1) to get:


Code:
8  5  6  7  8  9  0  1  3 
1  2  3  4  5  6  8  9  5
6  4  6  8  9  0  1  1  3  2
2  1  1  1  1  1  1  1 
10  6  8  2  4  8  2  8  0 
4  5  7  8  0  2  4  6  7

Is there a way to do it easily?
Thanks!

Last edited by cosmologist; 10-20-2011 at 07:51 AM..
# 2  
Old 10-20-2011
divide all the lines by line 4 ???

i guess, you provided the output (which is divided by 2 )
# 3  
Old 10-20-2011
Quote:
Originally Posted by itkamaraj
divide all the lines by line 4 ???

i guess, you provided the output (which is divided by 2 )
Because line 4 in this case is all 2s. (for simplicity)
# 4  
Old 10-20-2011
Code:
 
bash-3.00$ cat test
8  10  12  14  16  18  0  2  6
2   4    6    8    10  12  16  18  10  
6  8  12  16  18  0  2  2  6
2  2  2  2  2  2  2  2  2   
10  12  16  4  8  16  4  16  0
8  10  14  16  0  4  8  12  14
 
$ nawk '{for(i=1;i<=NF;i++){if(i==1){printf("%s ",$i)} else{printf("%s ",$i/2)} if(i==NF){printf("\n")}}}' test                                    
8 5 6 7 8 9 0 1 3 
2 2 3 4 5 6 8 9 5 
6 4 6 8 9 0 1 1 3 
2 1 1 1 1 1 1 1 1 
10 6 8 2 4 8 2 8 0 
8 5 7 8 0 2 4 6 7

This User Gave Thanks to itkamaraj For This Post:
# 5  
Old 10-20-2011
Thank you SOOOOOOOO much!! I really appreciate it Smilie

---------- Post updated at 07:26 AM ---------- Previous update was at 07:24 AM ----------

Quote:
Originally Posted by itkamaraj
Code:
 
bash-3.00$ cat test
8  10  12  14  16  18  0  2  6
2   4    6    8    10  12  16  18  10  
6  8  12  16  18  0  2  2  6
2  2  2  2  2  2  2  2  2   
10  12  16  4  8  16  4  16  0
8  10  14  16  0  4  8  12  14
 
$ nawk '{for(i=1;i<=NF;i++){if(i==1){printf("%s ",$i)} else{printf("%s ",$i/2)} if(i==NF){printf("\n")}}}' test                                    
8 5 6 7 8 9 0 1 3 
2 2 3 4 5 6 8 9 5 
6 4 6 8 9 0 1 1 3 
2 1 1 1 1 1 1 1 1 
10 6 8 2 4 8 2 8 0 
8 5 7 8 0 2 4 6 7


What if the 4th line is not all 2s? Is there as easy way to replace the "2" with like number 4? The file I gave was just to explain what I am trying to do, but the real file has different numbers.

---------- Post updated at 03:13 PM ---------- Previous update was at 07:26 AM ----------

I searched for an answer everywhere, and I tried so many things:

Code:
awk '{for(i=1;i<=NF;i++;A[i]=$i){if(i==1){printf("%s ",$i)} else{printf("%s ",$i/A[i])} if(i==NF){printf("\n")}}}' test

Code:
awk '{for(i=1;i<=NF;i++)A[i]=$i{if(i==1){printf("%s ",$i)} else{printf("%s ",$i/A[i])} if(i==NF){printf("\n")}}}' test

Code:
awk '{for(i=1;i<=NF;i++){if(i==1){printf("%s ",$i)} else{printf("%s ",$i/A[i])} if(i==NF){printf("\n")}}}' test

Code:
'{for(i=1;i<=NF;i++){if(i==1){printf("%s ",$i)} else{printf("%s ",$i/A[4])} if(i==NF){printf("\n")}}}' test

Code:
awk '{for(i=1;i<=NF;i++){if(i==1){printf("%s ",$i)} else{printf("%s ",$i/(i=4))} if(i==NF){printf("\n")}}}' test

Any help would be VERY highly appreciated Smilie Smilie

---------- Post updated at 03:14 PM ---------- Previous update was at 03:13 PM ----------

I searched for an answer everywhere, and I tried so many things:

Code:
awk '{for(i=1;i<=NF;i++;A[i]=$i){if(i==1){printf("%s ",$i)} else{printf("%s ",$i/A[i])} if(i==NF){printf("\n")}}}' test

Code:
awk '{for(i=1;i<=NF;i++)A[i]=$i{if(i==1){printf("%s ",$i)} else{printf("%s ",$i/A[i])} if(i==NF){printf("\n")}}}' test

Code:
awk '{for(i=1;i<=NF;i++){if(i==1){printf("%s ",$i)} else{printf("%s ",$i/A[i])} if(i==NF){printf("\n")}}}' test

Code:
'{for(i=1;i<=NF;i++){if(i==1){printf("%s ",$i)} else{printf("%s ",$i/A[4])} if(i==NF){printf("\n")}}}' test

Code:
awk '{for(i=1;i<=NF;i++){if(i==1){printf("%s ",$i)} else{printf("%s ",$i/(i=4))} if(i==NF){printf("\n")}}}' test

Any help would be VERY highly appreciated Smilie Smilie
# 6  
Old 10-20-2011
nawk -v row=4 -f cosmo.awk myFile
cosmo.awk:
Code:
NR==1 {ARGV[ARGC++] = ARGV[1]}
FNR==NR {if (row==FNR) split($0, rowA);next}
{
  for(i=1;i<=NF;i++)
    printf("%d%c", (i==1)?$i:$i/rowA[i], (i==NF)?ORS:OFS)
}

This User Gave Thanks to vgersh99 For This Post:
# 7  
Old 10-20-2011
Thank you SOOO much!! SmilieSmilieSmilieSmilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert row to columns start from nth column

Dear All, We have input like this: 161 57 1378 176 1392 262 1444 441 1548 538 1611 670 1684 241 57 1378 208 1393 269 1447 444 1549 538 1610 677 1700 321 ... (4 Replies)
Discussion started by: attila
4 Replies

2. Shell Programming and Scripting

Replace a value of Nth field of nth row

Using Awk, how can I achieve the following? I have set of record numbers, for which, I have to replace the nth field with some values, say spaces. Eg: Set of Records : 4,9,10,55,89,etc I have to change the 8th field of all the above set of records to spaces (10 spaces). Its a delimited... (1 Reply)
Discussion started by: deepakwins
1 Replies

3. Shell Programming and Scripting

Need help with awk statement to break nth column in csv file into 3 separate columns

Hello Members, I have a csv file in the format below. Need help with awk statement to break nth column into 3 separate columns and export the changes to new file. input file --> file.csv cat file.csv|less "product/fruit/mango","location/asia/india","type/alphonso" need output in... (2 Replies)
Discussion started by: awk-admirer
2 Replies

4. UNIX for Dummies Questions & Answers

Select 2 columns and transpose row by row

Hi, I have a tab-delimited file as follows: 1 1 2 2 3 3 4 4 a a b b c c d d 5 5 6 6 7 7 8 8 e e f f g g h h 9 9 10 10 11 11 12 12 i i j j k k l l 13 13 14 14 15 15 16 16 m m n n o o p p The output I need is: 1 1 a a 5 5 e e 9 9 i i 13... (5 Replies)
Discussion started by: mvaishnav
5 Replies

5. UNIX for Dummies Questions & Answers

Dividing up a text file by columns

Hi, I have a space de-limited text file with 1000 columns. I want to divide it up into 20 text files each with 50 columns, starting from the first column. How do I go about doing that? Thanks! (2 Replies)
Discussion started by: evelibertine
2 Replies

6. Shell Programming and Scripting

Dividing file into columns

Hi, I have a file containing 28048 lines and I would like to split it into a a file with 3506 lines and 8 columns? In column 1, I would like to have the first 3506 lines, in columns 2 the second 3506 lines and so on. I've been looking around in the forum but I found very specific cases. Is... (1 Reply)
Discussion started by: f_o_555
1 Replies

7. Shell Programming and Scripting

help [[row and columns]]

i ask to do ,,program that convert the last row to be the first row ,,,and after that exchange the the columns ex,, 1 2 3 4 5 6 7 8 9 to be 7 8 9 4 5 6 1 2 3 and then to be 9 8 7 6 5 4 3 2 1 give mee the code .... (0 Replies)
Discussion started by: khaled1989kh
0 Replies

8. UNIX for Dummies Questions & Answers

nth Columns in a Tab delimited file

Hi Can anyone help me to identify the nth field from a Tab delimited file? Thanks Subrat (8 Replies)
Discussion started by: subrat
8 Replies

9. Shell Programming and Scripting

File splitter by nth row

I need to split a file into n separate files of about the same size. The way the file will be split is at every nth row, starting with the first row, that row will be cut and copied to it's corresponding new file so that each file has unique records. Any 'leftovers' will go into the last file. e.g.... (4 Replies)
Discussion started by: sitney
4 Replies

10. UNIX for Dummies Questions & Answers

Row to Columns

Hi, I have a file like this. 1,1,1,0,0,0 1,1,2,1,0,0 1,1,3,0,0,0 1,1,4,0,0,0 ........... ........... 1,1,24,0,0,0 1,1,25,0,0,0 1,1,26,1,0,0 1,1,27,0,0,0 1,2,1,0,0,0 1,2,2,0,0,0 1,2,3,0,0,0 1,2,4,0,0,0 1,2,5,1,0,0 1,2,6,1,0,0 (4 Replies)
Discussion started by: vskr72
4 Replies
Login or Register to Ask a Question