0
a=0
b=1
for a in ${my_array[@]} 
   do
for b in ${my_array[@]}
   do
 if [ "  ${my_array[a]} " = "  ${my_array[b]} " ]
then 
 continue 
 ((a++)) 
fi

   done
  ((b++)) 
done

Hi. I want to compare 2 strings. They are in the same array. If they are same, I just print it one of them. How can I do that ? I write some code. There are 2 thins (a and b ) a's first value is 0 and it stores first element of array. b's first value is 1 and it stores 1.element of array. I want to compare them and if they are same strings, I just print one of them .so I use "continue". think my code is true, but it doesn't work .there is a mistake which I can't see. Can you help me ?

for example it runs like that .

 Enter words :
 erica 17
 sally 16
 john  18
 henry 17
 john 18
 jessica 19

as you see there are 2 john 18. I don't want both of them. My program will be check there are 2 strings are the same . If they are same I will just use one of them .

esrtr
  • 99
  • 2
  • 12
  • Can you provide a run example? I'm not quite sure I understood fully what you wanted. – Avihoo Mamka Oct 25 '15 at 12:44
  • Okey. I edited my question and and some runs part. can you look at ? @AvihooMamka – esrtr Oct 25 '15 at 12:47
  • as you see there are 2 john 18. I don't want both of them. My program will be check there are 2 strings are the same . If they are same I will just use one of them @AvihooMamka – esrtr Oct 25 '15 at 12:48
  • 1
    Possible duplicate of [How can I get unique values from an array in linux bash?](http://stackoverflow.com/questions/13648410/how-can-i-get-unique-values-from-an-array-in-linux-bash) – Avihoo Mamka Oct 25 '15 at 12:52
  • It likes this subject but and also I need how many times 2 inputs are same @AvihooMamka .I meas a user can write 3 times John 18. and I print it 1 John 18 but I need the strings match 3 times. This match numbers is important for me . – esrtr Oct 25 '15 at 12:58
  • You can do this: `echo "${my_array[@]}" | tr ' ' '\n' | sort | uniq -c | tr '\n' ' '` – Avihoo Mamka Oct 25 '15 at 13:02

3 Answers3

0

The if statment "=" - assign, "==" - compare.

Noproblem
  • 745
  • 1
  • 6
  • 15
0

If I understand correctly, you want to uniquify the elements of an array. If this is right, then the following stackoverflow question (How can I get unique values from an array in Bash?) appears to answer it in the following one-liner:

echo "${my_array[@]}" | tr ' ' '\n' | sort | uniq

Unfortunately, since your input words (or elements of array) contain spaces, the above will not work as expected. The issue is because the first echo will flatten-out the array into space-separated elements. The solution to this would be to use printf and remove 'tr'. Here it is...

printf '%s\n' "${my_array[@]}" | sort | uniq -c

But this alters the position of elements wrt the original array. Hope that is fine?

Community
  • 1
  • 1
Thejaswi
  • 26
  • 4
0

You can use sort and uniq to be able to get your desired output:

echo "${my_array[@]}" | tr ' ' '\n' | sort | uniq -c | tr '\n' ' '

And if you have spaces in your input you can use this:

printf '%s\n' "${my_array[@]}" | sort | uniq -c

That way you will get the number of times the string occurred but it will be printed just once.

Avihoo Mamka
  • 4,656
  • 3
  • 31
  • 44