Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

For loop and awk print

Shell Programming and Scripting


Closed    
 
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: 11 January 2016, 10:05 AM EST
Posts: 82
Thanks: 1
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: 27 June 2017, 8:12 PM EDT
Location: Amsterdam
Posts: 11,453
Thanks: 494
Thanked 3,301 Times in 2,913 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 Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 27 June 2017, 5:37 PM EDT
Location: Aachen, Germany
Posts: 10,969
Thanks: 280
Thanked 3,373 Times in 3,106 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 online now Forum Advisor  
Registered User
 
Join Date: May 2012
Last Activity: 28 June 2017, 2:55 AM EDT
Location: Simplicity
Posts: 3,582
Thanks: 282
Thanked 1,191 Times in 1,077 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 June 2017, 6:48 PM EDT
Location: Saskatchewan
Posts: 22,256
Thanks: 1,105
Thanked 4,200 Times in 3,885 Posts
Or just printf "%s\n" $tmpVar
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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:59 AM.