Shorter AWK for my code?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Shorter AWK for my code?
# 1  
Old 10-12-2010
Shorter AWK for my code?

Hi Folks,

I know my code works, but I'm still a newbie at arrays and how they function.

Is there is shorter way to write my code? I'm taking averages in multiple files and concatenating output into 1 file.

TIA!

Code:
for file in *; 
do 
awk -F"\t" '{a1+=$1}{a2+=$2}{a3+=$3} {a4+=$4}{a5+=$5} {a6+=$6} {a7+=$7} {a8+=$8}  {a9+=$9} {a10+=$10} {a11+=$11} {a12+=$12} {a13+=$13} {a14+=$14} {a15+=$15} {a16+=$16}  END 
	{print FILENAME, "\t"a1/(NR), "\t"a2/(NR), "\t"a3/NR, "\t"a4/NR, "\t"a5/NR, "\t"a6/NR, "\t"a7/NR, "\t"a8/NR, "\t"a9/NR, "\t"a10/NR,"\t"a11/NR,"\t"a12/NR,"\t"a13/NR,"\t"a14/NR, "\t"a15/NR,"\t"a16/NR}' 
	$file >> /Users/calitiggr/Desktop/S/S-$i
done

# 2  
Old 10-12-2010
Hi, it could be shortened like this:

Code:
for file in *; 
do
  awk -F '\t' '{for (i=1;i<=NF;i++) a[i]+=$i} END {printf FILENAME; for (i=1;i<=NF;i++) printf FS a[i]/NR;print ""}' "$file" >> /Users/calitiggr/Desktop/S/S-$i
done

or this:
Code:
awk -F '\t' 'function result() {printf f; for (i=1;i<=NF;i++) printf FS a[i]/p;print ""} 
             NR>1&&FNR==1{result()}{for (i=1;i<=NF;i++) a[i]+=$i;p=NR;f=FILENAME} END {result()}' * > /Users/calitiggr/Desktop/S/S-$i

This User Gave Thanks to Scrutinizer For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Just want to ask if there is a shorter hand to doing this one liner

Hi all, In Oracle, I am using SQL*Plus and selecting all rows in a table and spooling to a file as pipe delimited. I have to use pagesize 0 but unfortunately, using this option excludes the header and I can't get around having it to display the header fields. So to get around this, I have to... (2 Replies)
Discussion started by: newbie_01
2 Replies

2. Shell Programming and Scripting

Combining awk code into one

the following code works perfectly for me: # AWK 1 gawk -F, '/,'${ThisMonthDOW}' '${ThisMonthMON}' :: '${ThisMonthYEA}',/ { if (NF == 10) ... (6 Replies)
Discussion started by: SkySmart
6 Replies

3. Shell Programming and Scripting

Optimize awk code

sample data.file: 0,mfrh_green_screen,1454687485,383934,/PROD/G/cicsmrch/sys/unikixmain.log,37M,mfrh_green_screen,28961345,0,382962--383934 0,mfrh_green_screen,1454687785,386190,/PROD/G/cicsmrch/sys/unikixmain.log,37M,mfrh_green_screen,29139568,0,383934--386190... (7 Replies)
Discussion started by: SkySmart
7 Replies

4. Shell Programming and Scripting

Combine columns from many files but keep them aligned in columns-shorter left column issue

Hello everyone, I searched the forum looking for answers to this but I could not pinpoint exactly what I need as I keep having trouble. I have many files each having two columns and hundreds of rows. first column is a string (can have many words) and the second column is a number.The files are... (5 Replies)
Discussion started by: isildur1234
5 Replies

5. Shell Programming and Scripting

Help with AWK Code

hello, I would appreciate a little assistance with a process I'm trying to automate. I have several files that are zipped in central location, all follow the same naming conventions i.e (file 1, file 2, etc). what i would like to do is unzip the files and combined them into one file, basically... (2 Replies)
Discussion started by: mrn970
2 Replies

6. Shell Programming and Scripting

Need help with AWK code using xargs

Hi, I have a colon-separated file which contains names of users, among other details. My aim is to extract the line with the name and assign the name to a variable. A sample file is as follows -- ID: 123456 DEPARTMENT: xyz NAME: Bar, Foo Considering the Tabs between the colon and the... (6 Replies)
Discussion started by: Subu1987
6 Replies

7. Shell Programming and Scripting

Help in developing an awk code

#!/bin/ksh set -x FILENAME=$1 #To calculate debit and credit DEBIT=`awk -F, '{value=$28 ;if(value<0) {debit+=value}} END {print debit}' $FILENAME` CREDIT=`awk -F, '{value=$28 ;if(value>0) {credit+=value}} END {print credit}' $FILENAME` #This part is not working since the debit value is... (2 Replies)
Discussion started by: selvankj
2 Replies

8. Shell Programming and Scripting

Need help to understand Awk code.

Hi Guys, Can someone please explain this code to me. I could figure out it's adding and comparing two fields but I am not sure which ones. sort -t"|" -k3.1 /tmp/mpcashqc.xtr| awk -F"|" '{CHECKAMT+=$3;BATCHTOT=$4;\ items++}END{for(i in CHECKAMT) if (CHECKAMT!=BATCHTOT)... (6 Replies)
Discussion started by: nua7
6 Replies

9. Shell Programming and Scripting

Deleting shorter lines from data file

Hello, I have the following data file structure: 1234 text 2345 text 3456 text text text 4567 text text text 5678 text text text 6789 text text text I simply want to delete all of the lines that only have one text column (i.e. the first two lines in this case). The output would be:... (1 Reply)
Discussion started by: palex
1 Replies
Login or Register to Ask a Question