The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-28-2007
baghera baghera is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 23
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
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 (permalink)  
Old 08-28-2007
robotronic's Avatar
robotronic robotronic is offline Forum Advisor  
Can I play with madness?
  
 

Join Date: Apr 2002
Location: Italy
Posts: 370
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 (permalink)  
Old 08-28-2007
baghera baghera is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 23
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 (permalink)  
Old 08-28-2007
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,423
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 (permalink)  
Old 08-28-2007
baghera baghera is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 23
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..
  #6 (permalink)  
Old 08-28-2007
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,122
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..
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:12 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0