0

I'm trying to print a word from variable with multiple elements using array. Here is a script:

location=($(here mysql query to extract name of locations))
for i in "${location[@]}"; do
    echo "$i"
done

But these location name contains spaces: MUMBAI - BORIVALI, DELHI - LAJPATNAGAR and so on. So it prints like: Output in debug mode:

   + for i in '"${LOCATION[@]}"'
+ echo DELHI
DELHI
+ for i in '"${LOCATION[@]}"'
+ echo -
-
+ for i in '"${LOCATION[@]}"'
+ echo LAJPATNAGAR
LAJPATNAGAR
+ for i in '"${LOCATION[@]}"'
+ echo MUMBAI
MUMBAI
+ for i in '"${LOCATION[@]}"'
+ echo -
-
+ for i in '"${LOCATION[@]}"'
+ echo BORIVLI
BORIVLI

I've tried with double quotes:

location=("$(here mysql query to extract name of locations)")

then output is

+ for i in '"${LOCATION[@]}"'
+ echo 'DELHI - LAJPATNAGAR
MUMBAI - BORIVLI' 

together.

I want output:

+ echo 'DELHI - LAJPATNAGAR'
DELHI - LAJPATNAGAR
+ echo 'MUMBAI - BORIVLI'
MUMBAI - BORIVLI
5
  • Your edit shows commas between fields, is that correct? In that case you can set IFS to a comma. Please ensure that you show exactly what is produced by mysql. Commented May 16, 2018 at 7:08
  • @cdarke commas only in the question to show different location name Commented May 16, 2018 at 7:10
  • Your new edit shows hyphens between the fields, is that correct? Commented May 16, 2018 at 7:10
  • Why don't we eliminate the guessing and you post the first 10-lines of $(here mysql query to extract name of locations)? Commented May 16, 2018 at 7:21
  • yes there is hyphens and mysql query only contains the name of table and conditions. You can consider MUMBAI - BORIVLI and DELHI - LAJPATNAGAR as different value in column for better understanding. Commented May 16, 2018 at 7:25

3 Answers 3

1

If the locations don't contain newlines, you can try

while read -r location ; do
    echo "$location"
done < <(mysql ...)

read reads line by line if given just one parameter. By default, it reads from stdin, but with process substitution, we redirected output of the mysql command to it.

Sign up to request clarification or add additional context in comments.

1 Comment

@Lnux: Updated.
0

Try setting IFS to newline-only:

IFS="$(echo)"
location=($(here mysql query to extract name of locations))
for i in "${location[@]}"; do
    echo "$i"
done

Example with test.txt:

A B C
D E
F

Run the above code, replacing the MySQL query with cat test.txt. Output:

A B C
D E
F

If you drop the IFS="$(echo)" line, you will get each letter on a separate line.

4 Comments

No wait. .. the actual name of locations are different. Let me modify the questions. Its not llike 1 2
@Lnux Then just group them by three. If the number of spaces are not the same across all names, you're out of luck or need some other tricks.
@Lnux Updated and changed to another solution.
Output is still together
0

Just count 3 fields:

location=($(here mysql query to extract name of locations))

for ((i=0; i <= ${#location}; i+=3))
do
    echo "${location[i]} ${location[i+1]} ${location[i+2]}"
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.