我希望将一个文件的第一列与第二个文件的所有列进行比较,如果找到匹配,则打印第一列(第一文件)和在第二个文件中找到匹配的完整行。
示例输入file_1
RAM_1
RAM_2
RAM_3
RAM_4
RAM_5
RAM_6示例输入file_2
RAM_7 RAM_3
RAM_8 RAM_10 RAM_15 RAM_2
RAM_6 RAM_16 RAM_4
RAM_11 RAM_5 RAM_18 RAM_20 RAM_19
RAM_1 RAM_8 RAM_9 RAM_12预期产出
RAM_1 RAM_1 RAM_8 RAM_9 RAM_12
RAM_2 RAM_8 RAM_10 RAM_15 RAM_2
RAM_3 RAM_7 RAM_3
RAM_4 RAM_6 RAM_16 RAM_4
RAM_5 RAM_11 RAM_5 RAM_18 RAM_20 RAM_19
RAM_6 RAM_6 RAM_16 RAM_4我尝试过固定列的数量,但它只打印第一行文件。
awk 'NR==FNR{a[$1]=$0} $1 in a && $2 in a && $3 in a{print a[$1] ORS a[$2] ORS a[$3]}' file_2 file_1发布于 2020-06-10 07:00:57
请您试一试,基于所示的示例,用GNU awk编写。
awk '
FNR==NR{
a[$0]=$0
next
}
{
for(i=1;i<=NF;i++){
if($i in a){
print a[$i],$0 | "sort -k1"
}
}
}' file1 file2解释:添加了上面的详细说明。
awk ' ##Starting awk program from here.
FNR==NR{ ##Checking condition FNR==NR which will be TRUE when Input_file1 is being read.
a[$0]=$0 ##Creating an array named a with index current line and its value is current line.
next ##next will skip all further statements from here.
}
{
for(i=1;i<=NF;i++){ ##Going through all fields here in current line.
if($i in a){ ##checking condition if current field is inside array then do following.
print a[$i],$0 | "sort -k1" ##Printing value of array a with index of current field then current line printing here and sorting it by first field.
}
}
}' file1 file2 ##Mentioning Input_file names here.发布于 2020-06-10 07:16:22
另一种方法是,假设单词边界足以避免部分匹配,并且要匹配的文本没有regex元字符:
$ awk 'NR==FNR{a[$0]; next} {for(k in a) if(k ~ "\\<"$1"\\>") print $0, k}' f2 f1
RAM_1 RAM_1 RAM_8 RAM_9 RAM_12
RAM_2 RAM_8 RAM_10 RAM_15 RAM_2
RAM_3 RAM_7 RAM_3
RAM_4 RAM_6 RAM_16 RAM_4
RAM_5 RAM_11 RAM_5 RAM_18 RAM_20 RAM_19
RAM_6 RAM_6 RAM_16 RAM_4发布于 2020-06-10 11:17:29
这可能对您有用(GNU sed):
sed -E '1{x;s/^/cat file2/e;x};G;s/^(\S+)(\n.*)*\n([^\n]*\<\1\>[^\n]*).*/\1 \3/;P;d' file1在file1的开头,将file2复制到保持空间中。
对于file1中的每一行,追加file2并使用模式匹配和反向引用,生成一行包含来自file1的第一列和从file2生成的匹配行,或者只打印原始行。
https://stackoverflow.com/questions/62297528
复制相似问题