> 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-jiao-ji.md).

# 文件的交集

使用awk找出两个文件中所有都有的行。

文件1：

{% code title="file1" %}

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

{% endcode %}

文件2：

{% code title="file2" %}

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

{% endcode %}

找出这两个文件都有的行：

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

结果：

```bash
04J01
025J-01
```

将脚本保存在一个文件中：

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

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

{% endcode %}
