在序列中查找文件中的位置,其中已找到列表中的所有数字

时间:2016-11-24 10:18:52

标签: bash awk sed terminal

我可能错误地说了这个标题 - 我为此道歉。 我有一个(大)文本文件,其中包含数字列。我可以在命令行中从文件中提取列。我想把这个列输入到一个命令中,我给出一个数字列表,它告诉我列表匹配的位置(文件) - 意味着列表中的所有数字在列中至少出现过一次。

例如,我的列表中包含以下数字 1 2 3

提取的列是(注意我已经输入了不存在的行号)......

line1: 1
line2: 2
line3: 2
line4: 1
line5: 3
line6: 3
line7: 2

所以在这种情况下,它应该返回 5 (= line5)。

清晰的另一个例子......

我从文件中提取的列在换行符上具有以下顺序...

1 2 2 1 3 3 2 ...

我需要打印列表中所有数字都匹配的行号,我的列表是1 2 3。因此,在这种情况下,应该说第5行,此时它已经找到了所有1,2和3。

1 个答案:

答案 0 :(得分:1)

我的数据完全如上:

$ head -2 foo
line1: 1
line2: 2

在awk中。通过每一行,并用新号码记住最后一行。最后打印出来:

$ awk '
!($2 in a) {  # if the value has not been seen before
    a[$2];    # remember it in array a
    i=NR}     # also remember the number of record (NR) with unseen data
END {         # in the end
    print i   # print the i from above
}' foo
5

如果文件只有数字,而不是line1:等,请将$2更改为$1

修改

如果您想要为程序提供您想要查找的号码,请使用:

$ awk -v these="1 2 3" '  # pass the numbers to the program in variable
BEGIN {
    split(these,a," ")    # split them to a array
} 
($2 in a) {               # if found number is in a
    i=NR;                 # remember the NR
    delete a[$2]          # delete entry from array a
} 
END { print i }           # in the end print the last found NR
' foo
5

如果找不到所有数字,则失败并打印最后找到的数字的NR。可以通过以下方式实现:END { for (j in a) exit; print i}