[英]How can I write a file3 with different columns from a matched result queried from file1 to file2?
I have file1.txt and file2.txt with the following structure我有具有以下结构的 file1.txt 和 file2.txt
File1.txt文件1.txt
5511913332222
5511910000023
5511910000029
5511910000034
File2.txt文件2.txt
5511910000029|BLOCKED|7|30/07/2021 02:19:43
5511910000034|AVAIL|7|30/07/2021 03:11:53
5511910000048|AVAIL|7|30/07/2021 04:10:25
5511910000073|BLOCKED|7|30/07/2021 07:20:33
I want to write a file3.txt with the 1st and 2nd columns of the file2 where the 1st column matched the 1st column of file1.txt.我想用 file2 的第一列和第二列编写一个 file3.txt,其中第一列与 file1.txt 的第一列匹配。
File3.txt文件3.txt
5511910000029|BLOCKED
5511910000034|AVAIL
I have tried some tricks with awk but I couldn't get the expected result.我用 awk 尝试了一些技巧,但我无法得到预期的结果。 Could anyone please help me?有人可以帮我吗?
awk 'NR==FNR{a[$0]}NR>FNR && $0 in a{print}' file1 file2 > file3 awk 'NR==FNR{a[$0]}NR>FNR && $0 in a{print}' file1 file2 > file3
$ awk -F'|' -v OFS='|' 'NR==FNR {a[$1];next} ($1 && $1 in a){print $1,$2}' File1.txt File2.txt
5511910000034|AVAIL
To save output to File3.txt ... {print $1,$2 > "File3.txt"} ...
将输出保存到 File3.txt ... {print $1,$2 > "File3.txt"} ...
$ join -o 2.1,2.2 -t '|' -j 1 <(sort -k1 -t '|' file1.txt) <(sort -k1 -t '|' file2.txt)
5511910000029|BLOCKED
5511910000034|AVAIL
This means join
on the first field in both files ( -j 1
), outputting ( -o
) the first and second fields ( .1
, .2
) of the second file ( 2.
), using bar as a field delimiter ( -t '|'
), and sorting the inputs ( <(sort)
) on the first field ( -k1
) with bar as a delimiter (`-t '|' again).这意味着join
两个文件( -j 1
)中的第一个字段,输出( -o
)第二个文件( 2.
)的第一个和第二个字段( .1
, .2
),使用 bar 作为字段分隔符( -t '|'
),并在第一个字段( -k1
)上对输入( <(sort)
)进行排序,以 bar 作为分隔符(再次为`-t '|' )。
This assumes that file1.txt
and file2.txt
1) aren't already sorted;这假定file1.txt
和file2.txt
1) 尚未排序; 2) can be sorted; 2)可以排序; 3) don't have every other line blank, like you show in the question. 3)不要像您在问题中显示的那样,每隔一行空白。
As the lines in file1 consist of only digits, you can verify that using $0 ~ /^[[:digit:]]+$/
before setting them as a key in a
由于file1中的行仅包含数字,您可以在将它们设置为 a 中a
键之前使用$0 ~ /^[[:digit:]]+$/
验证
Set the output field separator to |
将输出字段分隔符设置为|
and set the field separator of file2 also to |
并将file2的字段分隔符也设置为|
awk -v OFS="|" '
NR==FNR && $0 ~ /^[[:digit:]]+$/ {
a[$0]
next
}
($1 in a){
print $1,$2
}
' file1 FS="|" file2 > file3
The content of file3 : file3的内容:
5511910000029|BLOCKED
5511910000034|AVAIL
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.