![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| while loop inside while loop | panknil | Shell Programming and Scripting | 0 | 01-07-2008 12:49 PM |
| For loop | xramm | HP-UX | 3 | 10-10-2007 02:20 PM |
| While Loop | hemangjani | Shell Programming and Scripting | 2 | 11-02-2006 11:01 AM |
| for loop | munnabhai1 | Shell Programming and Scripting | 3 | 04-06-2006 02:30 PM |
| how to get the similar function in while loop or for loop | trynew | Shell Programming and Scripting | 3 | 06-17-2002 11:09 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
While-loop with awk
Hi, I have recently posted in another thread started by me
. 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
columns: not found And I don't see the problem. Maybe there are some other issues with the script as well. Please enlighten me. |
|
||||
|
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
>> 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. |
|
||||
|
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 01:50 PM.. |
|
|||||
|
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 02:14 PM.. |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|