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

  Unix/Linux 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 Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 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 Unix and Linux 03-08-2013
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 28 March 2015, 12:25 PM EDT
Location: Amsterdam
Posts: 10,060
Thanks: 324
Thanked 2,630 Times in 2,351 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 Unix and Linux 03-08-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 28 March 2015, 2:37 PM EDT
Location: Aachen, Germany
Posts: 5,584
Thanks: 98
Thanked 1,462 Times in 1,376 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 Unix and Linux 03-08-2013
MadeInGermany MadeInGermany is offline Forum Advisor  
Registered User
 
Join Date: May 2012
Last Activity: 27 March 2015, 5:12 PM EDT
Location: Simplicity
Posts: 2,298
Thanks: 158
Thanked 699 Times in 630 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 Unix and Linux 03-08-2013
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 27 March 2015, 5:33 PM EDT
Location: Saskatchewan
Posts: 20,390
Thanks: 873
Thanked 3,575 Times in 3,340 Posts
Or just printf "%s\n" $tmpVar
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image 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 11:36 AM
awk to loop lines and print substring ux4me Shell Programming and Scripting 5 08-08-2012 07:19 AM
loop + sum + print using awk number10 Shell Programming and Scripting 1 07-21-2010 04:42 AM
How do I get awk to print a " in it's print part? LordJezo Shell Programming and Scripting 2 06-27-2006 09:16 PM



All times are GMT -4. The time now is 02:49 PM.