LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-10-2010, 12:47 AM   #1
skuz_ball
LQ Newbie
 
Registered: Nov 2007
Location: Melbourne, Australia
Posts: 14

Rep: Reputation: 0
while loop in awk function - syntax problem


I am having an issue getting the correct syntax right when I use a while loop inside an awk function. I am using ksh to basically multiply series of numbers that I have generated from a while loop with another number.

The following part of the script is where I encounter problems:

var=4

i=1
while ((i <= $var))

do

answer[i]=$(awk BEGIN'{print '$number[i]'*0.0174533}')
let i=i+1

done

The $number[i] variable has already been identified beforehand and works fine.

The error I am getting is as follows:

syntax error The source line is 1.
The error context is
BEGIN{print >>> [ <<<
awk: The statement cannot be correctly parsed.

The square brackets [ ] seem to be the issue but I have tried using a few different alternatives but nothing seems to work. Can anyone please suggest a way of making the above awk statement work? It would be much appreciated.
 
Old 03-10-2010, 01:25 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Hi skuz_ball

Assuming you are getting $number[i] from elsewhere in the script I think you need to
set the variable out side:

ie. awk -v num=${number[i]} BEGIN'{ print num * 0.0174533 }'
 
Old 03-10-2010, 02:59 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by grail View Post
Hi skuz_ball

Assuming you are getting $number[i] from elsewhere in the script I think you need to
set the variable out side:

ie. awk -v num=${number[i]} BEGIN'{ print num * 0.0174533 }'
That would work. Alternatively it could be substituted by its value at the same time as the shell expands the expression inside $() by using the necessary {} in the array reference:
Code:
$(awk BEGIN '{print '${number[i]}'*0.0174533}')
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] AWK Syntax MTK358 Programming 9 03-02-2010 11:31 AM
Problem with char driver write function - infinite loop Cherubim Linux - Kernel 1 10-13-2009 01:08 AM
cat command space problem with awk in for loop. shan_nathan Programming 3 06-18-2009 05:51 AM
awk , I need help for awk, just a display function mcandy General 1 12-15-2008 12:21 PM
AWK: problem getline inside while loop jjhall Programming 2 02-27-2008 03:19 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:04 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration