By Gengxin, 31 March, 2026
Forums

准备文件:geneID

配对的结果文件:

一共生成了1081个基因对

运行脚本:

 cat gene.pair.sh
#!/bin/bash

# 用法:bash all_pairs.sh 基因ID文件.txt 输出结果.txt

genelist=$1
outfile=$2

# 生成所有两两组合(不重复)
awk '
{
    for(i=1; i<NR; i++){
        print a[i] "\t" $0
    }
}
{ a[NR] = $0 }
' $genelist > $outfile

批量运行此目录下的所有txt文件,将文件中的geneID组成基因对

#!/bin/bash

# 功能:本目录下所有 .txt 文件 → 自动生成 两两基因对文件
# 输出文件:原文件名.pairs.txt

# 循环处理当前目录所有 txt 文件
for txt_file in *.txt; do
    # 跳过本身这个脚本,避免出错
    if [[ "$txt_file" == "$0" ]]; then
        continue
    fi

    # 输出文件名
    outfile="${txt_file%.txt}.pairs.txt"

    echo "正在处理:$txt_file  ->  输出:$outfile"

    # 生成两两基因对(你原来的核心代码)
    awk '
    {
        for(i=1; i<NR; i++){
            print a[i] "\t" $0
        }
    }
    { a[NR] = $0 }
    ' "$txt_file" > "$outfile"
done

echo -e "\n✅ 全部完成!所有txt都生成了基因对文件"