Addition of columns in a loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Addition of columns in a loop
# 1  
Old 10-08-2013
Addition of columns in a loop

I would like to add the numbers in 2nd column and 5th, 8th and 11th, and so forth till the end of the file then print them in one file.

Thanks,

Last edited by johnkim0806; 10-08-2013 at 10:54 AM..
# 2  
Old 10-08-2013
So, johnkim0806

You've been a member for a year or so, so what have you tried so far?
  • Do you have some input?
  • Is there any suggestion on what numbers you want to insert?
  • What language would you prefer to use (perl, awk, ksh, bash....)
How about trying:-
Code:
while read c1 c2 c3 c4 c5 c6 c7 c8 c9 c10 c11 c12
do
   echo "$c1 $RANDOM $c2 $c3 $RANDOM $c4 $c5 $c6 $c7 $RANDOM $c8 $c9 $c10 $RANDOM $c11"
done < inputfile > outputfile


It does what you ask, but it's a bit $RANDOM. Smilie

You are not giving us much to go on.



Robin
Liverpool/Blackburn
UK
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shell script, For loop output to excel as columns

Hi, I have a shell script which analyses the log folder for a specific string and throws me the output. I have used for loop since it does this in multiple servers. Now I want to save the output in a excel in the below format. Can someone please help? The output which I get Server1 : count... (14 Replies)
Discussion started by: srilaxman
14 Replies

2. Programming

awk to count occurrence of strings and loop for multiple columns

Hi all, If i would like to process a file input as below: col1 col2 col3 ...col100 1 A C E A ... 3 D E G A 5 T T A A 6 D C A G how can i perform a for loop to count the occurences of letters in each column? (just like uniq -c ) in every column. on top of that, i would also like... (8 Replies)
Discussion started by: iling14
8 Replies

3. Shell Programming and Scripting

How to loop my awk script across different columns?

input name a1 a2 a3 g1 0.8 0.4 0.2 g2 -0.2 -0.6 -0.7 g3 0.1 0.6 0.8 g4 0.1 0 0 g5 -0.2 -0.2 -0.2 g6 -0.1 -0.9 -0.9 g7 0 0 0.2 output a1:2 a2:0 a3:0 I use the following awk '{if ($2>=0.5) print $1"\t""1";... (3 Replies)
Discussion started by: quincyjones
3 Replies

4. Shell Programming and Scripting

Creating a loop for multiplying columns

I have 2 files, that look like this: ID SNP1 SNP2 SNP3 SNP4 A1 1 2 0 2 A2 2 0 1 1 A3 0 2 NA 1 A4 1 1 0 2 and this: SNP score SNP1 0.5 SNP2 0.7 SNP3 0.8 SNP4 0.2 Basically, all of the SNP-values are 0,1, 2 or NA, and they each have a score, listed in the second file. The total... (5 Replies)
Discussion started by: kayakj
5 Replies

5. Shell Programming and Scripting

Shell do loop to compare two columns (duplicates)

Hello, I have a single text file with three columns like: 2 1 10 3 6 11 4 4 12 5 5 13 6 8 14 I was wondering how I can run a shell script to (do loop) to read the first point in the first column , compare it with all the points in the second column and if they are the same,... (12 Replies)
Discussion started by: Paul Moghadam
12 Replies

6. Shell Programming and Scripting

Merging two columns from two files with similar names into a loop

I have two files like this: fileA.net A B C fileA.dat 1 2 3 and I want the output output_expected A 1 B 2 C 3 I know that the easier way is to do a paste fileA.net fileA.dat, but the problem is that I have 10,000 couple of files (fileB.net with fileB.dat; fileC.net with... (3 Replies)
Discussion started by: valente
3 Replies

7. UNIX for Dummies Questions & Answers

print columns of a file in a for loop

Hello, I have a file with 32 columns and I want to print one column at a time, and then read each line of this column in order to use grep command. I tried with awk but i cannot put variable in awk for a in {1..32..1};do awk '{print$a}' file1.txt|while read line;do grep $line file2.txt;done >... (5 Replies)
Discussion started by: FelipeAd
5 Replies

8. Shell Programming and Scripting

Arithmetic operation between columns using loop variable

Hi I have a file with 3 columns. say, infile: 1 50 68 34 3 23 23 4 56 ------- ------- I want to generate n files from this file using a loop so that 1st column in output file is (column1 of infile/(2*n+2.561)) I am doing like this: for ((i=1; i<=3; i++)) do a=`echo... (3 Replies)
Discussion started by: Surabhi_so_mh
3 Replies

9. Shell Programming and Scripting

Help needed: Adding columns in csv file in loop

Hi Everyone: My shell script creates multiple csv files (~30) in for loop. I want to compile (or merge) 3rd column from each (all) of these files to another file (in loop). Please help. Thanks. (3 Replies)
Discussion started by: smap007
3 Replies

10. Shell Programming and Scripting

Help with addition

Hi all, I am getting following output by using commands like sort, uniq and awk to the standard output. 110 d 40 a 59 c 9 b 3 e Now at the end I would like to add all the numbers in column 1 and display the count of all numbers i.e. (110 + 40 + 59 + 9 + 3). Also the output may... (3 Replies)
Discussion started by: tenderfoot
3 Replies
Login or Register to Ask a Question