LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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 05-02-2006, 08:51 AM   #1
rharvey@cox
Member
 
Registered: Aug 2003
Location: Victoria, Texas
Distribution: Gentoo
Posts: 63

Rep: Reputation: 15
awk "for loop" question


I'm going through O'Reilly Press' sed and awk by Dougherty&Robins and am having trouble writing a simple "for" loop.

I'm writing a script called acro that accepts parameters from the command line to search a data file

$ acro 1 2 3 4 5

I want each of the passed parameters to be searched through several fields of each record. My original solution to this was:

awk '$2 == search' search=$1 array
awk '$3 == search' search=$1 array
awk '$4 == search' search=$1 array
awk '$5 == search' search=$1 array
awk '$6 == search' search=$1 array

and so on for each of the command line parameters (search=$2 array, search=$3 array, search=$4 array, search=$5 array, search=$6 array).

I decided I wanted to write a loop to save some lines of code and came up with this:

for ( i=2; i=6; i++ )
awk '$i == search' search=$1 array

The above script fails with this error message:

$ ./acro 1 2 3 4 5 6
./acro: line 6: syntax error near unexpected token `i=6'
./acro: line 6: `for i=2; i=6; i++ '

I don't understand the error message. How can I use a "for" loop to get this job done?

Thanks.
 
Old 05-02-2006, 10:44 AM   #2
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 41
Are you trying to call awk from a shell script? If so, then it makes a difference which one you're using. (The for loops in Bourne shell, for example, only operate over lists.) If not, that for loop won't work the way you expect it to in Awk (or many other languages) because the conditional statement is wrong. You have an assignment to variable i, which since it is assigning 6 will always be true. So your loop would start at i=2 and continue going forever. You probably wanted "i <= 6" for your condition.
 
Old 05-02-2006, 02:14 PM   #3
rharvey@cox
Member
 
Registered: Aug 2003
Location: Victoria, Texas
Distribution: Gentoo
Posts: 63

Original Poster
Rep: Reputation: 15
Yes. "acro" is a shell script under BASH. I'll try your suggestion.

Thanks much.
 
Old 05-02-2006, 03:40 PM   #4
rharvey@cox
Member
 
Registered: Aug 2003
Location: Victoria, Texas
Distribution: Gentoo
Posts: 63

Original Poster
Rep: Reputation: 15
Well, changing to "for ( i=2; i<=6; i++ )" didn't work. What do you mean "loops in Bourne shell, for example, only operate over lists"? How do I use a list to accomplish my end?

Thanks.
 
Old 05-02-2006, 09:29 PM   #5
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 41
A vanilla "for" loop in the Bourne shell works like this:
Code:
for i in 1 2 3; do
    echo $i
done

# produces

1
2
3
So it may be easier to use a "while" loop instead:
Code:
i=1;
while [ $i -lt 4 ]; do
    echo $i
    i=$(($i+1))
done
which produces the same output. Basically, "for" loops traditionally represented iterators which moved over a predefined set, in this case the list. But in C, they were used more like traditional "while" loops, which has caused some confusion and lead to the introduction of the "foreach" loops in C-based languages like Perl. Anyways, in Bash it's possible to do arithmetic C-style "for" loops:
Code:
for (( i=1; i < 4; i++ )); do
    echo $i
done
produces the same output as the two above. But unfortunately, this will not work in Bourne shell (/bin/sh). Of course, on most Linux systems, /bin/sh is just a symlink to Bash, so it may not be an issue. You'll have to decide which is best for you, but the first form does have an advantage using a list, because the arguments specified on the command line are available in the $@ variable. So this bit of code will print out all the script's arguments:
Code:
for arg in $@; do
    echo $arg
done
Hope this helps.
 
Old 05-03-2006, 07:12 AM   #6
muha
Member
 
Registered: Nov 2005
Distribution: xubuntu, grml
Posts: 451

Rep: Reputation: 37
start learning over here: http://www.linuxcommand.org/
And post back if you have problems ..
 
  


Reply

Tags
bash, bourne, loops, shell


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
newbie question: whats the difference between "su root", "su" and "su -&quo mojarron Slackware 9 12-07-2009 05:08 PM
Replacing "function(x)" with "x" using sed/awk/smth Griffon26 Linux - General 3 11-22-2006 11:47 AM
how can I invoke "awk" from shell to do floating point math? Joseph Schiller Programming 8 01-12-2006 06:00 AM
awk/gawk handling of "delete" ? realos Programming 1 06-26-2003 10:45 AM
help with "next" command in "awk" realos Programming 2 05-13-2003 04:34 PM


All times are GMT -5. The time now is 03:27 AM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration