operating on all columns in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting operating on all columns in awk
# 1  
Old 12-17-2008
operating on all columns in awk

hi all, i am trying to write an awk script in which i want to perform calculations on each column in a file. i have this so far:

Code:
total[$i]=`awk '/ 1 / { j++ } {sum+=$i} END {print (sum/j)}' "$count".txt`
((count++))
((i++))

i am calculating the average for every column. i am trying to use $i as a variable to iterate across columns in the .txt file. the entire statement is in a loop.this is not working.
any help appreciated.
# 2  
Old 12-17-2008
Code:
awk '{ for(i=1; i<=NF; i++) 
            {arr[i]+=$i}
          recs++ 
       } 
       END {for (i in arr) {print i, arr[i]/recs}' inputfile

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 use "awk" to print columns from different files in separate columns?

Hi, I'm trying to copy and paste the sixth column from a bunch of files into a single file having each column pasted in separate columns (and not one after each other in just one column.) I tried this code but works only partially because it copied and pasted 50 rows of each column... (6 Replies)
Discussion started by: Frastra
6 Replies

2. Shell Programming and Scripting

Sum of columns using awk

Hello everyone I am a beginner in Shell scripting. Need your help to achieve desired result. I have a file (sample format below) 001g8aX0007jxLz xxxxxxxxxxxxxxx 9213974926411 CO-COMM-133 CO-L001-DLY 7769995578239 44938 1 1 ... (1 Reply)
Discussion started by: Rohit Mallah
1 Replies

3. Shell Programming and Scripting

Rearranging into new columns (awk?)

Hi experts, I've used several solutions from this forum to delete nonsense and rearrange data in the project file I'm working on. I'm hoping you guys can give me some tips on further rearranging the data (I've seen a few solutions by searching, but one specific item has me stumped, which is only... (5 Replies)
Discussion started by: coryvp
5 Replies

4. Shell Programming and Scripting

Transposing columns with awk

I want a sweet simple time efficient awk script in online which gets output 001_r 0.0265185 0.0437049 0.0240642 0.0310264 0.0200482 0.0146746 0.0351344 0.0347856 0.036119 1.49 firstcoloumnvalue allvaluesof 'c' in one row 001_r : 002_r c: 0.0265185 N: 548 001_r : 007_r c:... (5 Replies)
Discussion started by: phoenix_nebula
5 Replies

5. UNIX for Dummies Questions & Answers

Using awk to get columns from different files

How can I use awk to create a new file that has 2 columns, each colums comes form a different file. example: I need column 3 from file1 and column 5 from file2 to make file3. (3 Replies)
Discussion started by: cosmologist
3 Replies

6. Shell Programming and Scripting

cannot print the columns i want with awk.

hi friends! i have a script where a execute a veritas command, available_media wich retrieves me a list of tapes .lst then i execute cat /tmp/listtapes.lst | grep -v VL |sed '/^$/d'|awk -F, '{print $1, $3, $4, $9} ' > /tmp/media1.lst but it prints all the columns instead of the four... (3 Replies)
Discussion started by: pabloli150
3 Replies

7. Shell Programming and Scripting

how to AWK columns from $1 to $5 without $2 $3 $4

hello cant find a way to make something like: awk '{print $1 - $5}' somefile which is printing $1 $2 $3 $4 $5 should make an array or something? i just dont wanna write $1 $2 $3 $4 $5 to awk input i need to use from $1 to $5 and print them all and then i need to swith to: from $6 to $10 (3 Replies)
Discussion started by: tip78
3 Replies

8. Shell Programming and Scripting

match columns using awk

Hi All, I need some help in writing a small script using Awk. My input file has following deatils A,B,C,D 8239359,8239359,8388125,8388125 8239359,8239359,8388125,8388125 7165981,7165981,8363138,8363138 8283830,8283830,8382987,8382987 8209964,8209964,8367098,8367098 ... (8 Replies)
Discussion started by: pistachio
8 Replies

9. Shell Programming and Scripting

awk operating with shell vars only

Hi all How do I use awk such that it does not require an input file? I have a situation where I need to process some shell vars within awk (passed into awk with "-v VAR1=$VALUE1, VAR2=$VALUE2" etc), but that processing does not require/use an input file. Any guidance? TIA JG (2 Replies)
Discussion started by: jgrogan
2 Replies

10. Shell Programming and Scripting

List to columns and awk help

Hi I'm new to this forum and I'm a beginner when it comes to shell programming and awk programming. But I have the following problem: I've a list like this: 1 2 3 4 5 6 7 8 Either from a file or output from a command. What I would like to do is to arrange these values into x columns... (17 Replies)
Discussion started by: baghera
17 Replies
Login or Register to Ask a Question