> For the complete documentation index, see [llms.txt](https://valineliu.gitbook.io/deuterium-wiki/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://valineliu.gitbook.io/deuterium-wiki/linux/awk-1/wen-jian-de-cha-ji.md).

# 文件的差集

找出所有文件A有但是文件B没有的行。

文件1：

{% code title="file1" %}

```bash
034J
025J-01
045k
089G-02
04J01
```

{% endcode %}

文件2：

{% code title="file2" %}

```bash
04J01
025J-01
038L-02
```

{% endcode %}

找出所有file1有但是file2没有的行：

```bash
awk 'NR==FNR {lines[$0]=1;next} !lines[$0]' file2 file1
```

结果：

```bash
034J
045k
089G-02
```

需要注意两个文件参数的位置，file1-file2就是file1在后file2在前；file2-file1就是file1在前file2在后：

```bash
awk 'NR==FNR {lines[$0]=1;next} !lines[$0]' file1 file1
```

结果：

```bash
038L-02
```

脚本保存成一个文件：

{% code title="file\_difference.awk" %}

```bash
#! /usr/bin/awk -f
NR == FNR { lines[$0]=1; next } !lines[$0]
```

{% endcode %}
