bashshellcomm

Compare two lists of strings in bash


I have two lists of dates in bash and I want to find the difference between them. Trying to use comm, but I can't work out how to compare two stored lists of strings (as opposed to command output or files).

A bit like this:

list1=$(seq 3 | xargs -I {} gdate -d "{} days ago" +%Y-%m-%d)
list2=$(seq 7 | xargs -I {} gdate -d "{} days ago" +%Y-%m-%d)

comm -1 -3 <"$list1" <"$list2"

# 2020-03-29
# 2020-03-28
# 2020-03-27: No such file or directory

Where this produces the desired output:

comm -1 -3 <(seq 3 | xargs -I {} gdate -d "{} days ago" +%Y-%m-%d) <(seq 7 | xargs -I {} gdate -d "{} days ago" +%Y-%m-%d)

# 2020-03-26
# 2020-03-25
# 2020-03-24
# 2020-03-23

Clearly comm is trying to use the output as file locations, instead of content, but I can't figure out how to fix it.


Solution

  • As comm requires two files, this is what's needed :

    list1="$(seq 3 | xargs -I {} gdate -d "{} days ago" +%Y-%m-%d)"
    list2="$(seq 7 | xargs -I {} gdate -d "{} days ago" +%Y-%m-%d)"
    
    comm -1 -3 <(printf "%s\n" "$list1") <(printf "%s\n" "$list2")