shell/bash 交集、并集、差集

方法一(直接用文件名):取两个文本文件的并、交、差集
并:
sort -m <(sort file1 | uniq) <(sort file2 | uniq) | uniq
交:
sort -m <(sort file1 | uniq) <(sort file2 | uniq) | uniq -d
差 file1 - file2:
sort -m <(sort file1 | uniq) <(sort file2 | uniq) <(sort file2 | uniq) | uniq -u

 ## /bin/bash 可用; /bin/sh 不可用

 

方法二(用变量参数):取两个文本文件的并、交、差集

file1=XXXX

file2=YYYY


# 并:
sort -m <(sort $file1 | uniq) <(sort $file2 | uniq) | uniq
# 交:
sort -m <(sort $file1 | uniq) <(sort $file2 | uniq) | uniq -d
# 差 file1 - file2:
sort -m <(sort $file1 | uniq) <(sort $file2 | uniq) <(sort $file2 | uniq) | uniq -u

  ## /bin/bash 可用; /bin/sh 不可用

 

方法三:

file1=XXXX

file2=YYYY

# 并:

cat $file1 $file2 | sort | uniq

# 交:

cat $file1 $file2 | sort | uniq -d

 

 

备注:

uniq -d 会输出重复行 duplicate

uniq -u 只显示唯一的行 uniq

 

posted @ 2013-12-23 22:36  emanlee  阅读(3671)  评论(0编辑  收藏  举报