How can I create a set-like array in bash?

awm129

Suppose I have the following list of files:

/aaa/bbb/file1.txt
/aaa/ccc/file2.txt
/aaa/bbb/file3.txt
/aaa/bbb/file4.txt
/aaa/ccc/file5.txt

And I'd like to have a set of all of the unique dirnames in an array. The resulting array would look something like this:

dirs=( "/aaa/bbb" "/aaa/ccc" )

I think I can do something like this, but it feels really verbose (pardon the syntax errors, i don't have a shell handy):

dirs=()
for f in filelist do
    dir=$(dirname $f)
    i=0
    while [$i -lt ${#dirs[@]} ]; do
        if [ dirs[$i] == $dir ]
            break
        fi
        i=$[i + 1]
    done
    if [ $i -eq ${dirs[@]} ]
        dirs+=($dir)
    fi
 done
konsolebox

Use associative arrays:

declare -A dirs

for f in "${filelist[@]}"; do
    dir=$(exec dirname "$f") ## Or dir=${f%/*}
    dirs[$dir]=$dir
done

printf '%s\n' "${dirs[@]}"

Or if input is from file:

readarray -t files < filelist
for f in "${files[@]}"; do
    dir=$(exec dirname "$f") ## Or dir=${f%/*}
    dirs[$dir]=$dir
done
  • Let's keep unnecessary forks on a subshell minimum with exec.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I set characters like "{" in urls?

From Dev

How can I create layout like this?

From Dev

How can i create a categorized list like this

From Dev

How can I create a matrix so I can access each number from the array like so $array[$key]

From Dev

How can I update a single child document in a MongoDB document array using something like $set and $elemMatch?

From Dev

How can I update a single child document in a MongoDB document array using something like $set and $elemMatch?

From Java

How can I create an array in Kotlin like in Java by just providing a size?

From Dev

How can I create a simple like button thats based on stored user IDs in an array?

From Dev

How to create a function that can sort an array in bash?

From Dev

How can I create a stopwatch in bash?

From Java

How can I join elements of an array in Bash?

From Dev

How can I make zsh completion behave like Bash completion?

From Dev

How can I use a Bash-like shell on Windows?

From Dev

How can I have bash-like password input in BATCH?

From Dev

How can I create a custom type to always represent an array of some basic type with a set number of elements?

From Dev

How can I convert a Set to an Array in TypeScript

From Dev

How can I set split string in array?

From Dev

Is there anything like "maxQueryTime"? and if yes then How can I set mongo maxQueryTime?

From Dev

How can i set an arduino pin act like a jumper?

From Dev

how can i set view like alert and hide automatically in iOS

From Dev

How can I create an array of functions?

From Java

How can I create a stream from an array?

From Dev

How can I create an array of classes in Swift

From Dev

How can I create numpy array of views?

From Dev

How can I Create an array with N zeros?

From Dev

How can I create an array with polymorphic data?

From Dev

How can I create std::function with array?

From Dev

how can i create a table for this array?

From Dev

How can I create array of lines in this case?

Related Related

  1. 1

    How can I set characters like "{" in urls?

  2. 2

    How can I create layout like this?

  3. 3

    How can i create a categorized list like this

  4. 4

    How can I create a matrix so I can access each number from the array like so $array[$key]

  5. 5

    How can I update a single child document in a MongoDB document array using something like $set and $elemMatch?

  6. 6

    How can I update a single child document in a MongoDB document array using something like $set and $elemMatch?

  7. 7

    How can I create an array in Kotlin like in Java by just providing a size?

  8. 8

    How can I create a simple like button thats based on stored user IDs in an array?

  9. 9

    How to create a function that can sort an array in bash?

  10. 10

    How can I create a stopwatch in bash?

  11. 11

    How can I join elements of an array in Bash?

  12. 12

    How can I make zsh completion behave like Bash completion?

  13. 13

    How can I use a Bash-like shell on Windows?

  14. 14

    How can I have bash-like password input in BATCH?

  15. 15

    How can I create a custom type to always represent an array of some basic type with a set number of elements?

  16. 16

    How can I convert a Set to an Array in TypeScript

  17. 17

    How can I set split string in array?

  18. 18

    Is there anything like "maxQueryTime"? and if yes then How can I set mongo maxQueryTime?

  19. 19

    How can i set an arduino pin act like a jumper?

  20. 20

    how can i set view like alert and hide automatically in iOS

  21. 21

    How can I create an array of functions?

  22. 22

    How can I create a stream from an array?

  23. 23

    How can I create an array of classes in Swift

  24. 24

    How can I create numpy array of views?

  25. 25

    How can I Create an array with N zeros?

  26. 26

    How can I create an array with polymorphic data?

  27. 27

    How can I create std::function with array?

  28. 28

    how can i create a table for this array?

  29. 29

    How can I create array of lines in this case?

HotTag

Archive