How to loop through space separated values?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to loop through space separated values?
# 1  
Old 12-15-2011
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

# 2  
Old 12-15-2011
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?
# 3  
Old 12-15-2011
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
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

This User Gave Thanks to Corona688 For This Post:
# 5  
Old 12-15-2011
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 02:19 PM.. Reason: formatted for easy reading
# 6  
Old 12-15-2011
Quote:
Originally Posted by kchinnam
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 02:23 PM..
# 7  
Old 12-15-2011
Code:
for file in '/<folderA>/SapRFCLogs_A.*'  '/<folderB>/SapRFCLogs_B.*'
do
        for f in $file
        do
                echo $f
        done
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to get the values of multipledot(.) separated fields?

Hello, I have a file which has the following contents : thewall............0000000000200000 kmemfreelater......0000000000000000 kmemgcintvl........0000000000000002 kmeminuse..........00000000223411C0 allocated..........0000000029394000 bucket.......... @.F1000A02800C2158 The mentioned... (4 Replies)
Discussion started by: rahul2662
4 Replies

2. Shell Programming and Scripting

Need Help on For Loop to pass space separated value as one value

Hi, I am having a file say list1 with a output like below jun 12 18:23 may 20 18:23 Now i want to pass the above two values into for loop,I have written a script like this. #!/bin/bash a=`cat list1` for i in $a do echo "HI $i" done expected output: HI jun 12 18:23 (3 Replies)
Discussion started by: sumanthupar
3 Replies

3. Shell Programming and Scripting

Read record from the text file contain multiple separated values & assign those values to variables

I have a file containing multiple values, some of them are pipe separated which are to be read as separate values and some of them are single value all are these need to store in variables. I need to read this file which is an input to my script Config.txt file name, first path, second... (7 Replies)
Discussion started by: ketanraut
7 Replies

4. UNIX for Dummies Questions & Answers

How convert space separated list to matched columns?

Hi I have been racking my (limited) brains to get this to work without success I have a file output which is a list of lists - ie a single column of data that is separated by space into sub lists below - I need to both split this so that each list is in a separate column (eg tab or semicolon... (8 Replies)
Discussion started by: Manchesterpaul
8 Replies

5. Shell Programming and Scripting

Needs help in parsing comma separated values

hello experts, i am retrieving values in variables jobKey and jobName within my shell script. these values are returned to me within braces and i am using following command to remove those braces: jobKeys=`echo $jobKeys | sed 's:^.\(.*\).$:\1:'` jobNames=`echo $jobNames | sed... (1 Reply)
Discussion started by: avikaljain
1 Replies

6. UNIX for Dummies Questions & Answers

[solved] Comma separated values to space separated

Hi, I have a large number of files which are written as csv (comma-separated values). Does anyone know of simple sed/awk command do achieve this? Thanks! ---------- Post updated at 10:59 AM ---------- Previous update was at 10:54 AM ---------- Guess I asked this too soon. Found the... (0 Replies)
Discussion started by: lost.identity
0 Replies

7. Shell Programming and Scripting

To agregate Comma separated values

Hi pls help me to get the code: i have a file in which content is : 2.01304E+11 2.01304E+11 ori 2 01:00 2.01304E+11 2.01304E+11 ori 2 01:02 2.01304E+11 2.01304E+11 ori 3 01:02 2.01304E+11 2.01304E+11 ori 3 ... (7 Replies)
Discussion started by: Aditya.Gurgaon
7 Replies

8. UNIX for Dummies Questions & Answers

Get all values separated with spaces(solved)

Hi, i have this text: X (m) 4917536.9627 4917536.9673 0.0090 -0.0046 Y (m) -815726.1383 -815726.1294 0.0061 -0.0089 Z (m) 3965857.4730 3965857.4840 0.0071 -0.0110 X (m) 4917536.9627 4917537.1411 -0.1784 0.1710 Y (m) -815726.1383 -815726.4859 0.3476 0.3489 Z (m) 3965857.4730... (2 Replies)
Discussion started by: limadario
2 Replies

9. Shell Programming and Scripting

Passing space separated value to a function - error

Taking inputs in the script which is space separated and passing this to a function and i have assigned like below And then when I use for loop for the inputs i got from the user, it is taking only the first argument. Enter Names : Bala Sundar Sridhar read names namesCheck $names function... (7 Replies)
Discussion started by: balamv
7 Replies

10. Shell Programming and Scripting

Extracting the values separated by comma

Hi, I have a variable which has a list of string separated by comma. for ex , Variable=/usr/bin,/usr/smrshbin,/tmp How can i get the values between the commas separately using shell scripts.Please help me. Thanks, Padmini. (6 Replies)
Discussion started by: padmisri
6 Replies
Login or Register to Ask a Question