LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 09-02-2010, 02:57 AM   #1
babaqga
LQ Newbie
 
Registered: Aug 2010
Posts: 18

Rep: Reputation: 1
awk for-loop


Hi guys,

I am writing a simple bash script looking like this:
LOOP1
Code:
#!/bin/bash
...
echo $OUTPUT | awk '{
for (i="'"$NRCORES"'";i<="'"$ENDLOOP"'";i++)
print $i
}'
My problem is that the script doesn't output anything
BUT !
If I change it to the following:
LOOP2
Code:
#!/bin/bash
echo $OUTPUT | awk '{
for (i=8;i<=17;i++)
print $i
}'
It gives me what it is supposed to give (the 8-th to 17-th column of the $OUTPUT variable)

Strangely, if I echo $NRCORES or $ENDLOOP in the script I get 8 and 17 respectively, so in my view LOOP1 is exactly the same as LOOP2. However, awk doesn't seem to agree with me. In addition, I know for sure that when awk encounters a non-numeric variable in the print statement, it will take it as a zero and print the whole string. Therefore, I don't see what the problem can be.
WHAT IS WRONG WITH IT ?
please respond

Last edited by babaqga; 09-02-2010 at 03:03 AM.
 
Old 09-02-2010, 03:02 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

This works:
Code:
#!/bin/bash

OUTPUT="1234567890"
NRCORES=4
ENDLOOP=6

echo $OUTPUT | awk '{
for (i='$NRCORES';i<='$ENDLOOP';i++)
print i
}'
Mind the quoting and the print i (not $i)

Hope this helps.
 
Old 09-02-2010, 03:08 AM   #3
babaqga
LQ Newbie
 
Registered: Aug 2010
Posts: 18

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by druuna View Post
Hi,

This works:
Code:
#!/bin/bash

OUTPUT="1234567890"
NRCORES=4
ENDLOOP=6

echo $OUTPUT | awk '{
for (i='$NRCORES';i<='$ENDLOOP';i++)
print i
}'
Mind the quoting and the print i (not $i)

Hope this helps.
Yep, it helped, with the exception I want not the i itself, but the $i. Anyway, you were right for the quoting.

Last edited by babaqga; 09-02-2010 at 03:09 AM.
 
Old 09-02-2010, 03:17 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Which $i are you talking about?

The for loop with the i=x;i<=y;i++ part is were i is generated (not $i). This is how awk works.

Maybe you should tell us what it is you are trying to accomplish so we can come up with code that suits that.

Hope this helps.
 
Old 09-02-2010, 03:57 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
use the -v option of awk to pass in shell variables to awk. Its "preferred" , than messy quoting.
Code:
#!/bin/bash
...
NRCORES="something"
ENDLOOP="something"

echo $OUTPUT | awk -vnc="$NCORES" -v el="$ENDLOOP" '{
for (i=nc;i<=el;i++)
  print $i
}'
 
  


Reply


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
for loop in awk command jssidhu4 Programming 7 03-31-2010 04:19 AM
awk loop related kalyanofb Programming 3 02-16-2007 02:31 AM
awk in loop How to Nkunzis Linux - Newbie 3 12-10-2006 01:34 PM
awk: loop through ldap aliases tonedeaf1969 Programming 6 11-17-2006 11:56 AM
awk - simple 'for' loop doing my nut... davee Programming 2 06-30-2004 08:54 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 10:39 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