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: 19 December 2014, 1:14 AM EST
Location: Amsterdam
Posts: 9,690
Thanks: 300
Thanked 2,491 Times in 2,230 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: 19 December 2014, 6:56 AM EST
Location: Aachen, Germany
Posts: 4,886
Thanks: 80
Thanked 1,224 Times in 1,150 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: 18 December 2014, 1:32 AM EST
Location: Simplicity
Posts: 2,089
Thanks: 140
Thanked 613 Times in 556 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 online now Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 19 December 2014, 4:36 PM EST
Location: Saskatchewan
Posts: 20,030
Thanks: 855
Thanked 3,454 Times in 3,226 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 05:37 PM.