简体   繁体   English

如何从从 file1 查询到 file2 的匹配结果中编写具有不同列的 file3?

[英]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.txtfile2.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.

相关问题 如何遍历两个文件并逐行查找file1中匹配file2的所有匹配项,然后替换为file3中的内容 - How to iterate over two files and find all occurrences in file1 matching file2, line by line, then replace with content from file3 将带有一列的 file1 与来自 file2 的两列进行比较 - Compare file1 with one column to two columns from file2 从File2中提取行已找到File1 - Extract lines from File2 already found File1 Perl 使用 file2 从 file1 中删除单词 - Perl removing words from file1 with file2 如果在file2中找不到file1的输出行 - Output line from file1 if not found in file2 将文件 1 中的数据追加/补充到文件 2 (linux) - Append/supplement data from file1 to file2 (linux) 我想从 file1 中的第 1 列和第 2 列中找到一些与 file2 中的第 1 列匹配的字符串/单词,并替换为 file2 中的第 2 列字符串/单词 - I want to find some strings/words from column 1 and 2 in file1 that match column 1 in file2 and replace with column 2 strings/words in file2 如何基于文件/ file1(仅)第一列与linux中的file2的匹配信息从file1提取行? - how to extract rows from file1 based on matching information of its/file1 (only)first column with file2 in linux? 我有两个巨大的序列文件,我想从文件 2 中的文件 1 中提取相同的行号 - I have two huge sequencefiles where i want to extract the same linenumbers from file1 in file2 打印文件1与文件2的差异,而不从文件2中删除任何内容 - print differences of file1 to file2 without deleting anything from file2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM