For loop and awk print | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

For loop and awk print

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-08-2013
onesuri onesuri is offline
Registered User
 
Join Date: Feb 2008
Last Activity: 13 February 2014, 1:57 PM EST
Posts: 77
Thanks: 0
Thanked 0 Times in 0 Posts
For loop and awk print

Hi,

i am using a variable tmpVar, using variable data i need implement for loop


Code:
tmpVar="abc bbc cbc nbc mbc"    # valiable having total number of words= 5 so i need to loop 5 times. 


tmpVar="abc bbc cbc nbc mbc" 
tmpcnt=`echo $tmpVar|wc -w`
for cnt in 1..$tmpcnt
do
t1=`echo $tmpVar|awk -F" " '{print $cnt}'`      # this is not working. it should take the value first word in the string then process 
echo $t1
#checking if t1 = abc then we are implementing some logic and so on....
-
-
-
-
done

Sponsored Links
    #2  
Old 03-08-2013
Scrutinizer's Avatar
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 28 November 2014, 7:51 PM EST
Location: Amsterdam
Posts: 9,627
Thanks: 294
Thanked 2,459 Times in 2,205 Posts
If you are using bash or ksh93 , try:

Code:
for cnt in {1..$tmpcnt}

But this is not a very efficient way of doing this..
Sponsored Links
    #3  
Old 03-08-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 28 November 2014, 12:29 PM EST
Location: Aachen, Germany
Posts: 4,719
Thanks: 80
Thanked 1,173 Times in 1,102 Posts
Why do you use that difficult loop construct?
Code:
for t1 in "$tmpvar"; do ...; done

will loop through all five substrings in your tmpvar. If you need a count inside the loop, set and use one, like (( cnt++ )) .
    #4  
Old 03-08-2013
MadeInGermany MadeInGermany is offline Forum Advisor  
Registered User
 
Join Date: May 2012
Last Activity: 28 November 2014, 3:08 AM EST
Location: Simplicity
Posts: 2,055
Thanks: 134
Thanked 603 Times in 547 Posts
Oh dear...and ' $cnt ' does not substitute the shell variable.
Better a simple for loop

Code:
tmpVar="abc bbc cbc nbc mbc"
for t1 in $tmpVar
do
 echo "$t1"
done

Sponsored Links
    #5  
Old 03-08-2013
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 28 November 2014, 4:08 PM EST
Location: Saskatchewan
Posts: 19,944
Thanks: 848
Thanked 3,422 Times in 3,207 Posts
Or just printf "%s\n" $tmpVar
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Print in New line in loop navsan Shell Programming and Scripting 7 12-10-2012 12:36 PM
awk to loop lines and print substring ux4me Shell Programming and Scripting 5 08-08-2012 08:19 AM
loop + sum + print using awk number10 Shell Programming and Scripting 1 07-21-2010 05:42 AM
How do I get awk to print a " in it's print part? LordJezo Shell Programming and Scripting 2 06-27-2006 10:16 PM



All times are GMT -4. The time now is 11:32 PM.