Bash set subtraction

后端 未结 4 424
长发绾君心
长发绾君心 2021-01-11 10:48

How to subtract a set from another in Bash?

This is similar to: Is there a "set" data structure in bash? but different as it asks how to perform the subtra

4条回答
  •  温柔的废话
    2021-01-11 11:40

    uniq -u (manpage) is often the simplest tool for list subtraction:

    Usage

    uniq [OPTION]... [INPUT [OUTPUT]] 
    [...]
    -u, --unique
        only print unique lines
    

    Example: list files found in directory a but not in b

    $ ls a
    file1  file2  file3
    $ ls b
    file1  file3
    
    $ echo "$(ls a ; ls b)" | sort | uniq -u
    file2
    

提交回复
热议问题