While-loop with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting While-loop with awk
# 1  
Old 08-28-2007
While-loop with awk

Hi, I have recently posted in another thread started by me Smilie. But in an effort to make my script more beautiful I've been thinking abbout while loops.

I run my script with the command:

sh script 4 numbers.txt

And my script is like this:

Code:
data=`cat $2 | xargs -n $1`

#echo $data

columns=$1
i=1

while columns>0
do
	awk_cmd=`awk '{sum+=$i} END {print "\n" sum/NR}'`
   	echo "$data | $awk_cmd"
	i=`expr $i + 1`
	columns=`expr $columns - 1`
done

The whole point of the script is to take a list ($2) and produce $1 numbers of columns and then print the average of each column. But I get an error when executing the script:

columns: not found

And I don't see the problem. Maybe there are some other issues with the script as well. Please enlighten me.
# 2  
Old 08-28-2007
First, the test condition is wrong. Use this:

Code:
while [ "$columns" -gt 0 ]

Second, if you want to pass the "$i" value to awk try this:

Code:
awk_cmd=`awk -v i=$i '{sum+=i} END {print "\n" sum/NR}'`

# 3  
Old 08-28-2007
Ok I did as you said:

Code:
data=`cat $2 | xargs -n $1`

#echo $data

columns=$1
i=1
echo $columns

while [ "$columns" -gt 0 ]
do
	awk_cmd=`awk -v i=$1 '{sum+=$i} END {print sum/NR}'`
   	echo "$data | $awk_cmd"
	i=`expr $i + 1`
	columns=`expr $columns - 1`
done

But then I get this kind of error:

>> sh reader4 4 testfil.txt
awk: syntax error near line 1
awk: bailing out near line 1
2 2 1 1
1 1 1 1 |
awk: syntax error near line 1
awk: bailing out near line 1
2 2 1 1
1 1 1 1 |
awk: syntax error near line 1
awk: bailing out near line 1
2 2 1 1
1 1 1 1 |
awk: syntax error near line 1
awk: bailing out near line 1
2 2 1 1
1 1 1 1 |

What I want is that is goes through each column on each run in the while loop and prints the corresponding average. But I really appreciate the help, I have been to some forums where you get slammered because, maybe, stupid questions.
# 4  
Old 08-28-2007
You don't need the awk_cmd command :
Code:
data=`cat $2 | xargs -n $1`

#echo $data

columns=$1
i=1
echo $columns

while [ "$columns" -gt 0 ]
do
   	echo "$data" | awk -v i=$1 '{sum+=$i} END {print sum/NR}'
	i=`expr $i + 1`
	columns=`expr $columns - 1`
done

If you want to keep it, you must use the eval command :
Code:
	awk_cmd="awk -v i=$1 '{sum+=$i} END {print sum/NR}'"
	echo "$data" | eval $awk_cmd

Jean-Pierre.
# 5  
Old 08-28-2007
Thanks Jean-Pierre, now I don't get any errors. But the output is wierd.

If I have the list:

1
2
1
2

This would produce:
1 2
1 2

So the output should be 1 and 2 (1+1/2 and 2+2/2 ), like so:
1
2

But instead I get

2
2

Something is wrong with the way I'm going about this problem.

echo "$data" | awk -v i=$1 '{sum+=$i} END {print sum/NR}'

Is there something wrong with using NR here? But putting a number there doesn't seem to help. I tried with 2 instead of NR but (same list as above) but still I get:

2
2

Is there a problem with the division I'm making?

Edit: I just echoed data and the output comes in one line 1 2 1 2 so probably is has something to do with my problem.

Last edited by baghera; 08-28-2007 at 02:50 PM..
# 6  
Old 08-28-2007
I don't quiet understand why you need all the coding when you can do it all in awk:

nawk -f bag.awk myFile

bag.awk:
Code:
{
   for(i=1; i<=NF; i++)
       sum[i] += $i
   nf=NF
   nr=NR
}

END {
    for(i=1; i<=nf; i++)
        print sum[i]/nr
}


Last edited by vgersh99; 08-28-2007 at 03:14 PM..
# 7  
Old 08-28-2007
Quote:
Originally Posted by vgersh99
I don't quot understand why you need all the coding when you can do it all in awk:

nawk -f bag.awk myFile

bag.awk:
Code:
{
   for(i=1; i<=NF; i++)
       sum[i] += $i
   nf=NF
   nr=NR
}

END {
    for(i=1; i<=nf; i++)
        print sum[i]/nr
}

Huh? I tried your script but I only get 1.5

when I have list:

1
2
1
2

The output should be:
1
2

Or on the same row 1 2

As I described above. But there should also be the possibility to tell how many columns there should be.

Last edited by baghera; 08-28-2007 at 03:05 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk !seen[$]++ in else loop

Hi all, I was searching the net for a solution for my problem... unfortunately nothing so far. I want to sort on more than on column tab delimited file and keep the line if in the column I sort there is no value, but for those who have a value I want them only unique. I have tried the... (5 Replies)
Discussion started by: ksenia
5 Replies

2. UNIX for Beginners Questions & Answers

awk with For loop

Hi My Requirement is to take the sum of each column below is the input file. 1 2 3 4 1 2 3 4 1 2 3 4 Initial i was using below command to achieve my desired result. however this was adding the row and not column. i am not able understand why this is happening awk... (1 Reply)
Discussion started by: scriptor
1 Replies

3. Shell Programming and Scripting

awk programming -Passing variable to awk for loop

Hi All, I am new to AWK programming. I have the following for loop in my awk program. cat printhtml.awk: BEGIN -------- <some code here> END{ ----------<some code here> for(N=0; N<H; N++) { for(M=5; M<D; M++) print "\t" D ""; } ----- } ... (2 Replies)
Discussion started by: ctrld
2 Replies

4. Shell Programming and Scripting

awk loop using array:wish to store array values from loop for use outside loop

Here's my code: awk -F '' 'NR==FNR { if (/time/ && $5>10) A=$2" "$3":"$4":"($5-01) else if (/time/ && $5<01) A=$2" "$3":"$4-01":"(59-$5) else if (/time/ && $5<=10) A=$2" "$3":"$4":0"($5-01) else if (/close/) { B=0 n1=n2; ... (2 Replies)
Discussion started by: klane
2 Replies

5. Shell Programming and Scripting

awk loop and using shell in awk

Hi, everyone! I have a file, when I print its $1 out it show several strings like this: AABBCC AEFJKLFG FALEF FAIWEHF What I want to do is that, after output of each record, search the string in all files in the same folder, print out the record and file name. This is what I want... (4 Replies)
Discussion started by: xshang
4 Replies

6. Shell Programming and Scripting

awk - loop from a to z

Hello, I was wondering if it is possible to do a loop on letters rather than numbers with awk (gawk). Basically I used to do: echo "nothing" | gawk '{for(i=1;i<11;i++)print i}' But I would like to do something like that (which obviously does not work): echo "nothing" | gawk '{for(i in... (6 Replies)
Discussion started by: jolecanard
6 Replies

7. Shell Programming and Scripting

Comparison and editing of files using awk.(And also a possible bug in awk for loop?)

I have two files which I would like to compare and then manipulate in a way. File1: pictures.txt 1.1 1.3 dance.txt 1.2 1.4 treehouse.txt 1.3 1.5 File2: pictures.txt 1.5 ref2313 1.4 ref2345 1.3 ref5432 1.2 ref4244 dance.txt 1.6 ref2342 1.5 ref2352 1.4 ref0695 1.3 ref5738 1.2... (1 Reply)
Discussion started by: linuxkid
1 Replies

8. Shell Programming and Scripting

awk for-loop and NR

Hey, I know this is a stupid question, but it doesn't work. I have a file with 10 lines and I want to pipe the content to awk and then print line 1 til 2 into another file and then line 3-4 ... So my script looks like that, but doesn't work: cat grid_ill.pts | awk '{ for (NR=1;NR<3;NR++)... (8 Replies)
Discussion started by: ergy1983
8 Replies

9. UNIX for Dummies Questions & Answers

for loop in awk?

I am new to unix and have pieced together two scripts that work independently. The first checks all the filesystems and reports which are running low on space. df -m | awk 'int($4) > 75 { print $1 " has only " $3 "mb free from a total of " $2 ", this filesystem is " $4 " full! \n" }... (1 Reply)
Discussion started by: Bdawk
1 Replies

10. Shell Programming and Scripting

Using AWK in a for loop

Hello, I am trying to use AWK to print only the first field of numerous text files, and then overwrite these files. They are of the format 1*2,3,4,5. I have tried the following code (using tcsh): foreach f (file1 file2 file3) cat $f | awk -F'*' '{print $1}' > $f end However, I get very... (4 Replies)
Discussion started by: Jahn
4 Replies
Login or Register to Ask a Question