Awk 提取边缘列表中的特定行

Awk 提取边缘列表中的特定行,awk,Awk,我有一个包含数百万条无向边的大型网络文件: edge.txt带有节点1、节点2和一些数字属性 给定另一个文件input.txt a b c a b 0.8 b c 0.1 a c 0.1 只有在input.txt中同时具有节点(node1和node2)的边才应打印 a b c a b 0.8 b c 0.1 a c 0.1 我试过: awk 'FNR==NR {a[$0]++;next}{if ($1 in a && $2

我有一个包含数百万条无向边的大型网络文件: edge.txt带有节点1、节点2和一些数字属性

给定另一个文件input.txt

a
b
c
a   b   0.8
b   c   0.1
a   c   0.1
只有在input.txt中同时具有节点(node1和node2)的边才应打印

a
b
c
a   b   0.8
b   c   0.1
a   c   0.1
我试过:

awk 'FNR==NR {a[$0]++;next}{if ($1 in a && $2 in a) {print}}' input.txt edge.txt

需要一些建议吗?谢谢

您可以试试这个
awk
脚本:

awk 'NR==FNR{a[$1];next}($1 in a)&&($2 in a)' input.txt edge.txt

数组查找需要括在括号内,并且不需要
print
语句(因为这是条件有效时的默认语句)。

另一个尝试最小化对哈希
b
的查找的语句,如果edge>>输入:

$ awk '
NR==FNR && !($1 in a) {   # if node not in hash a yet, ie. remove duplicates in input
    for(i in a) {         # "c" -> a[]: insert to b: ca, ac, cb, bc
        b[$1 i]
        b[i $1]
    }
    a[$1]                 # new entries go to a as well
    next
}
($1 $2 in b) {
    # delete b[$1 $2]     # uncomment these to remove duplicates
    # delete b[$2 $1]     # ie. "a b 0.8" vs. "b a 0.8"
    print
}' input edge  # if both $1 and $2 are in a, $1 $2 is in b
输出:

a   b   0.8
b   c   0.1
c   b   0.1
b   a   0.8
a   c   0.1
删除重复项后:

a   b   0.8
b   c   0.1
a   c   0.1

@oliv的方法是正确的,但是如果你想移除转置对,你需要添加更多的条件

$ awk 'NR==FNR{a[$1]=1; next} a[$1] && a[$2] && !b[$1,$2]++ && !b[$2,$1]++' input edge

a   b   0.8
b   c   0.1
a   c   0.1