1

您好我有两个文件如下: File1.txt

10   A   B  C
10   A   D  K
11   X   Y  Z
10   A   K  B 
11   Y   X  A 

文件2.txt

10
11
12

预期的输出是

10 A  B  C  A  D  K  A  K  B
11 X  Y  Z  Y  X  A 

我试过命令grep -f File1.txt file2.txt

但它并没有给我所有价值都在相同的id下

4

4 回答 4

3

请您尝试以下操作。

awk '
FNR==NR{
  val=$1
  $1=""
  sub(/^ +/,"")
  a[val]=(a[val]?a[val] OFS:"")$0
  next
}
($1 in a){
  print $1,a[$1]
}
' File1.txt File2.txt

输出如下:

10 A B C A D K A K B
11 X Y Z Y X A

说明:为上述代码添加详细说明。

awk '                                ##Starting awk program from here.
FNR==NR{                             ##Checking condition if FNR==NR which will be true for first file1.
  val=$1                             ##Creating variable val with $1 value of it.
  $1=""                              ##Nullify $1 here.
  sub(/^ +/,"")                      ##Substituting initial space with NULL for current line.
  a[val]=(a[val]?a[val] OFS:"")$0    ##Creating array a with index val and keep concatenating its value to it.
  next                               ##next will skip further statements from here.
}
($1 in a){                           ##Checking condition if $1 of current line comes in array a then do following.
  print $1,a[$1]                     ##Printing $1 of current line and value of array a with $1 value.
}
' File1.txt File2.txt                ##Mentioning Input_file names here.
于 2020-02-12T07:24:10.860 回答
1

如果密钥在第二个文件中,您可以将该行添加到字典中。
例子:

from collections import defaultdict

d = defaultdict(list)

with open("f1.txt") as f1, open("f2.txt") as f2:
    keys = set(f2.read().splitlines())
    for line in f1:
        k, *rest = line.split()
        if k in keys:
            d[k]+=rest


>>> print(*d.items(),sep="\n")
('10', ['A', 'B', 'C', 'A', 'D', 'K', 'A', 'K', 'B'])
('11', ['X', 'Y', 'Z', 'Y', 'X', 'A'])
于 2020-02-12T07:31:51.613 回答
1

我在这里看不到 File2.txt 的相关性。因此,如果输出的间距不重要,您可以使用此 1 行命令:

sort File1.txt | awk '$1!=key {if (sum) print key sum; key=$1; sum=""} {$1=""; sum=sum $0} END {print key sum}'
于 2020-02-12T08:02:27.630 回答
1

你可以试试这个:

with open("File1.txt") as f1, open("File2.txt") as f2:
    dictf1 = {}
    for i in f1.readlines():
        i = i.split()
        if i[0] in dictf1.keys():
            dictf1[i[0]] += i[1:]
        else:
            dictf1[i[0]] = i[1:]
    for i in f2.readlines():
        if i[:-1] in dictf1.keys():
            print(i[:-1], " ".join(dictf1[i.strip()]))
于 2020-02-12T07:51:51.770 回答