-1

I am reading data from a file line by line and storing it to array, so that I can access later that array to compare with other files data.

Now, my input file has redundancy, how to add only unique lines to array. I need to check while adding to array itself?

Below is the code I am using to read data from file:

while read line
do
    //How to check array already contains this line???
        <Code to adding to array>
done <"$file"
SpringUser
  • 45
  • 5
  • What version of bash? How are you planning to use this array later? – Etan Reisner Jul 22 '15 at 17:41
  • Duplicate: http://stackoverflow.com/questions/3685970/check-if-an-array-contains-a-value – Brian Jul 22 '15 at 17:41
  • @springuser you should check to see if anyone asked the same trivial question previously. You'll enlighten yourself with wisdom of the ancients of StackOverflow. – msw Jul 23 '15 at 04:40

1 Answers1

0

You can use associative array for this:

declare -A ulines

while IFS= read -r line; do
   [[ ! ${ulines["$line"]} ]] && echo "$line" && ulines["$line"]=1
done < "$file"

This will print unique lines on stdout preserving original order in input file.

anubhava
  • 664,788
  • 59
  • 469
  • 547