Awk 有没有办法找到编号字符串中的重复模式?
我有个问题要问。我对此进行了研究,但没有找到答案 我想知道是否可以执行一个命令或脚本来读取编号字符串列表,并在同一行中找到至少两个或更多重复的模式 例如,下面是一个示例列表Awk 有没有办法找到编号字符串中的重复模式?,awk,Awk,我有个问题要问。我对此进行了研究,但没有找到答案 我想知道是否可以执行一个命令或脚本来读取编号字符串列表,并在同一行中找到至少两个或更多重复的模式 例如,下面是一个示例列表 1 5 3 1 6 7 9 4 1 7 5 2 4 6 1 5 1 0 7 3 在第1、3和4行中可以找到1和5的模式 有没有一种方法可以在不告诉我要寻找什么数字的情况下找到这些模式 awk '/1/ && /5/' file 谢谢,如果无法提供任何意见或反馈,我将不胜感激。不完全理解请求,但此脚本可作为
1 5 3 1
6 7 9 4
1 7 5 2
4 6 1 5
1 0 7 3
在第1、3和4行中可以找到1和5的模式
有没有一种方法可以在不告诉我要寻找什么数字的情况下找到这些模式
awk '/1/ && /5/' file
谢谢,如果无法提供任何意见或反馈,我将不胜感激。不完全理解请求,但此脚本可作为后续行动的基础
$ awk '{for(i=1;i<=NF;i++)
{for(j=1;j<NR;j++)
if((j,$i) in a)
{c[NR,j]++; v[NR,j]=v[NR,j] FS $i}
a[NR,$i]}}
END {for(i=1;i<=NR;i++)
for(j=i+1; j<=NR; j++)
if(c[j,i]>1) print "("i","j"):" v[j,i]}' file
(1,3): 1 5
(1,4): 1 5
(1,5): 1 3
(2,4): 4 6
(3,4): 1 5
(3,5): 1 7
$awk'{for(i=1;i假设每行中的数字顺序很重要,这里有一种通用的方法,可以使用GNU awk为数组数组、长度(数组)
和排序的
获得一些数字的组合(由下面的变量r
指定):
$ cat tst.awk
###################
# Calculate all combinations of a set of strings, see
# https://rosettacode.org/wiki/Combinations#AWK
###################
function get_combs(A,B, i,n,comb) {
## Default value for r is to choose 2 from pool of all elements in A.
## Can alternatively be set on the command line:-
## awk -v r=<number of items being chosen> -f <scriptname>
n = length(A)
if (r=="") r = 2
comb = ""
for (i=1; i <= r; i++) { ## First combination of items:
indices[i] = i
comb = (i>1 ? comb OFS : "") A[indices[i]]
}
B[comb]
## While 1st item is less than its maximum permitted value...
while (indices[1] < n - r + 1) {
## loop backwards through all items in the previous
## combination of items until an item is found that is
## less than its maximum permitted value:
for (i = r; i >= 1; i--) {
## If the equivalently positioned item in the
## previous combination of items is less than its
## maximum permitted value...
if (indices[i] < n - r + i) {
## increment the current item by 1:
indices[i]++
## Save the current position-index for use
## outside this "for" loop:
p = i
break
}
}
## Put consecutive numbers in the remainder of the array,
## counting up from position-index p.
for (i = p + 1; i <= r; i++) indices[i] = indices[i - 1] + 1
## Print the current combination of items:
comb = ""
for (i=1; i <= r; i++) {
comb = (i>1 ? comb OFS : "") A[indices[i]]
}
B[comb]
}
}
# Input should be a list of strings
{
split($0,A)
delete B
get_combs(A,B)
for (comb in B) {
combs[comb][NR]
}
lines[NR] = $0
}
END {
PROCINFO["sorted_in"] = "@ind_str_asc"
for (comb in combs) {
if ( length(combs[comb]) > 1 ) {
print comb, "is in lines:"
PROCINFO["sorted_in"] = "@ind_num_asc"
for (lineNr in combs[comb]) {
print "\t" lineNr ":", lines[lineNr]
}
}
}
}
有关更多信息,请参阅。我在理解您的问题时遇到困难。请问“编号字符串”是什么?与“两个或多个”和“同一行上的重复模式”有何不同…与什么相同的行?谢谢。另外,我们是否应该在第3行和第5行中找到1和7?数字是否总是个位数,或者是否可以73
或1874
出现在某个地方?是否总是正好有4列?第1行中的3
和1
是否应该与最后一行中的1
和3
匹配ne?第2行和第4行也有2个公共元素。数字的顺序重要吗?我的意思是,如果第4行包含4651
,而不是4615
,那么第2行和第4行是否仍被视为匹配模式因为第2行包含6 4
,而第4行包含4 6
,即顺序错误?
$ awk -f tst.awk file
1 3 is in lines:
1: 1 5 3 1
5: 1 0 7 3
1 5 is in lines:
1: 1 5 3 1
3: 1 7 5 2
4: 4 6 1 5
1 7 is in lines:
3: 1 7 5 2
5: 1 0 7 3