1

I tried to look through a lot of similar questions but I have a specific query. I have two or more sets of strings (space separated values). I want to loop through

firstString="f1 f2 f3 f4"
secondString="s1 s2 s3 s4"

I want something like

f1-s1
f2-s2
f3-s3
f4-s4

(in a single loop)

I must be able to take the positional value of the second and further arrays in a single loop.

4
  • Have you tried associative arrays? (bash >=4.0) Commented Oct 3, 2011 at 18:10
  • 1
    possible duplicate of bash - determine a substring in one variable when an associated substring is known in another variable Commented Oct 3, 2011 at 20:04
  • I'm not sure if it's an exact duplicate, but that the answers from that question should help answer this one as well. Commented Oct 3, 2011 at 20:57
  • Associative array is a very good concept and could also be used in similar issues(depending on the type of requirement). Thanks for the info. Commented Oct 4, 2011 at 8:58

4 Answers 4

2

Well, if you first replace all spaces with a new-line, using tr so that you have each value on a separate line, then paste will solve your problem:

$ cat a b
f1
f2
f3
f4
s1
s2
s3
s4

$ paste -d- a b
f1-s1
f2-s2
f3-s3
f4-s4

Pure bash solution:

#!/bin/bash

firstString='f1 f2 f3 f4'
secondString='s1 s2 s3 s4'

read -ra FIRST <<< "$firstString"
read -ra SECOND <<< "$secondString"

index=0
for i in ${FIRST[@]}
do
    echo $i-${SECOND[$index]}
    ((index++))
done
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the solution. I was looking for the pure bash solution you gave as I can take in as puch pairs of string as I wish. The non-pure solution also works for known inputs :)
1

You could make use of bash built-in arrays:

first=(f1 f2 f3 f4)
second=(s1 s2 s3 s4)
for (( i = 0; i < ${#first[*]}; i++ )); do
    echo ${first[$i]}-${second[$i]}
done

3 Comments

Although we know the input. they are not in the form of arrays but strings. Also note that it can be more than 2 sets of string
@explorer: The input does not need to be an array already. If they're strings you simply put their content inside the parantheses: str="f1 f2 f3 f4"; strArr=($str). It's that simple. As for the more than 2 sets, just add "third", "fourth" and so on... :)
This is a very elegant way of creating arrays. Thanks for this useful tip.
0

see the test with awk below:

kent$  firstStr="f1 f2 f3 f4"
kent$  secondStr="s1 s2 s3 s4"

#now we have two variable


kent$  echo 1|awk -v two=$secondStr -v one=$firstStr '{split(one,a);split(two,b);for(i=1;i<=length(a);i++)print a[i]"-"b[i]}' 
f1-s1
f2-s2
f3-s3
f4-s4

1 Comment

The solution is very useful but I am loking for two or more pair of strings in which case this might be a little diffcult to use
0

You can easily do it in a portable manner:

set $firstString
for s in $secondString; do
  echo "$1-$s"
  shift
done

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.