How to loop through space separated values? | 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.

How to loop through space separated values?

Shell Programming and Scripting


Tags
loop thru space seperated values.

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-15-2011
kchinnam kchinnam is offline
Registered User
 
Join Date: Jul 2008
Last Activity: 29 April 2014, 10:34 PM EDT
Location: US
Posts: 142
Thanks: 15
Thanked 2 Times in 2 Posts
How to loop through space separated values?

How do I loop thru space separated values in a variable?
I hate to use very complicated counter increment logic for this kind of simple problem.

Expected result(using ksh)

Code:
$>echo "aaa bbbb cccc" | <looping code here>
var=aaa
var=bbbb
var=cccc


Code:
$>echo "aaa bbbb cccc" | while IFS=" " read var; do echo "var=$var"; done
var=aaa bbbb cccc
$>echo "aaa bbbb cccc" | while IFS=' ' read var; do echo "var=$var"; done
var=aaa bbbb cccc
$>echo "aaa bbbb cccc" | while IFS=\  read var; do echo "var=$var"; done 
var=aaa bbbb cccc
$>echo "aaa bbbb cccc" | while IFS="\ " read var; do echo "var=$var"; done
var=aaa bbbb cccc

Sponsored Links
    #2  
Old 12-15-2011
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
Moderator
 
Join Date: Jan 2007
Last Activity: 1 September 2014, 10:03 AM EDT
Location: Варна, България / Milano, Italia
Posts: 5,667
Thanks: 182
Thanked 617 Times in 575 Posts

Code:
% printf 'var=%s\n' aaa bbbb cccc  
var=aaa
var=bbbb
var=cccc

If you really need a loop for some reason:


Code:
% set -- aaa bbbb cccc 
% for w; do printf 'var=%s\n' "$w"; done
var=aaa
var=bbbb
var=cccc

Note that set -- will reset your positional parameters (i.e. you'll loose them).

By the way, what exactly are you trying to achieve?
Sponsored Links
    #3  
Old 12-15-2011
kshji's Avatar
kshji kshji is offline
Registered User
 
Join Date: Jun 2009
Last Activity: 26 February 2014, 5:44 AM EST
Location: Finland
Posts: 602
Thanks: 0
Thanked 50 Times in 49 Posts
Here is some ideas

Code:
a=$(echo "aaa bbbb cccc"   )
set -- $a
while [ $# -gt 0 ]
do
        echo "var=$1"
        shift
done

#####
arr=( $(echo "aaa bbb ccc") )
while [ ${#arr[@]} -gt 0 ]
do
        echo "var=${arr[0]}"
        set -A arr -- ${arr[@]:1}
done

####
echo aaa bbb ccc | some.sh

#some.sh:
while [ $# -gt 0 ]
do
        echo "var=$1"
        shift
done

    #4  
Old 12-15-2011
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 31 August 2014, 3:15 PM EDT
Location: Saskatchewan
Posts: 19,271
Thanks: 774
Thanked 3,237 Times in 3,035 Posts
I think this is being overthought somewhat...


Code:
var="string with spaces"
for X in $var # note that $var must NOT be quoted here!
do
        echo "$X"
done

The Following User Says Thank You to Corona688 For This Useful Post:
radoulov (12-15-2011)
Sponsored Links
    #5  
Old 12-15-2011
kchinnam kchinnam is offline
Registered User
 
Join Date: Jul 2008
Last Activity: 29 April 2014, 10:34 PM EDT
Location: US
Posts: 142
Thanks: 15
Thanked 2 Times in 2 Posts
I am using KSH version M-11/16/88i. Following command works only when there are no quotes around variables. I don't think I can use it in my case..

Quote:
$>
set -- "aaa bbbb cccc"; for w; do printf 'var=%s\n' "$w"; done
var=aaa bbbb cccc
Quote:
By the way, what exactly are you trying to achieve?
I am trying to process sets of files[ file1 file2 ..] [ passed as a arguments from first loop, and process each file in second loop.


Code:
SAP_A='/<folderA>/SapRFCLogs_A.*'   # This will resolve to few files
SAP_B='/<folderB>/SapRFCLogs_B.*'    # This will resolve to few files

set -A RFC_Fldrs "SAP_A" "SAP_B"

for file in "${RFC_Fldrs[@]}"
do
        vTemp=$(eval echo \$$file)   # this will pass underlying array variable values to vTemp.
       
        # This line will go into function call once command works..<functioncall> <$vTemp>
         echo $vTemp | while IFS=" " read file1; do echo $file1; done     # Why this IFS=" " does not work here!
done

About other solutions with code more than 3 lines.
I can't believe we have to reinvent the wheel for such a simple task by writing 25 lines of code.. I will be ashamed of using Shells..

Last edited by kchinnam; 12-15-2011 at 01:19 PM.. Reason: formatted for easy reading
Sponsored Links
    #6  
Old 12-15-2011
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 31 August 2014, 3:15 PM EDT
Location: Saskatchewan
Posts: 19,271
Thanks: 774
Thanked 3,237 Times in 3,035 Posts
Quote:
Originally Posted by kchinnam View Post
About other solutions with code more than 3 lines.
I can't believe we have to reinvent the wheel for such a simple task by writing 25 lines of code.. I will be ashamed of using Shells..
Your question was phrased a little awkwardly. You got what you asked for, but perhaps not what you wanted -- I don't see a reason why you actually need to use a pipe there at all. The pipe is what's making it so complicated.

Given that, did you try my solution?


Code:
var="string with spaces"
# This kind of string splitting is controlled by IFS too!
for X in $var # note that $var must NOT be quoted here!
do
        echo "$X"
done


Last edited by Corona688; 12-15-2011 at 01:23 PM..
Sponsored Links
    #7  
Old 12-15-2011
kshji's Avatar
kshji kshji is offline
Registered User
 
Join Date: Jun 2009
Last Activity: 26 February 2014, 5:44 AM EST
Location: Finland
Posts: 602
Thanks: 0
Thanked 50 Times in 49 Posts

Code:
for file in '/<folderA>/SapRFCLogs_A.*'  '/<folderB>/SapRFCLogs_B.*'
do
        for f in $file
        do
                echo $f
        done
done

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
Input Validation of comma separated values LinuxRacr Shell Programming and Scripting 5 05-25-2011 04:16 PM
need help extracting values from string separated by a delimiter adshocker Shell Programming and Scripting 3 02-21-2011 08:57 PM
Get all values separated with spaces(solved) limadario UNIX for Dummies Questions & Answers 2 01-27-2011 06:51 AM
Extracting the values separated by comma padmisri Shell Programming and Scripting 6 07-31-2009 03:13 AM
Splitting comma separated values into an array tmarikle Shell Programming and Scripting 3 06-24-2005 05:50 PM



All times are GMT -4. The time now is 07:25 PM.