英文:
Join two files and get the matching records on shell scripting
问题
我有两个文件,两个文件都由分隔符分隔,我们需要将这两个文件连接起来,将匹配的记录发送到out_file文件中。在第一个文件中,关键列是第1列,在第二个文件中,关键列是第3列,而在out_file中,我只需要第一个文件的所有列和第二个文件的第6列。
示例文件
注意:原始文件有90多个记录,而且两个文件都没有排序)
文件1:
用户名| 状态| 管理员姓名
4115 | Open | X
234 | Expired |Y
文件2:
Id| 名称| 用户名| 类型| 角色| 访问
1| 用户1| 4115| primary| connect| Never
2| 用户|432|Nonuser|conect|Never
输出文件
用户名| 状态| 管理员姓名| 访问
4115 | Open | X |Never
我尝试了这个但它给出了来自两个文件的所有记录:
join -a 2 -t "|" -1 1 -2 3 file1 file2>out_file
英文:
I have two files both files are separated by delimiter we need to join these two files and send the matching records to out_file on the first file key column is 1st column and in the second file 3rd column is the key column and on out_file i need all the columns from first file and only 6th columns from second file
Sample files
Note:Original files has 90+ records and both the files are unsorted)
File1:
Username| Status| Managername
4115 | Open | X
234 | Expired |Y
File2:
Id| Name| Username| type| role| Access
1| User1| 4115| primary| connect| Never
2|User|432|Nonuser|conect|Never
OutputFile
Username| Status| Managername| Access
4115 | Open | X |Never
I have tried this but this is giving all the records from both the files:
join -a 2 -t "|" -1 1 -2 3 file1 file2>out_file
答案1
得分: 2
awk -F' *[|] *' 'FNR==NR { a[$3]=$6; next } $1 in a { print $0 " | " a[$1] }' File2 File1
prints
Username | Status | Managername | Access
4115 | Open | X | Never
Note that File2
is specified first.
Explanation:
-F' *[|] *'
sets|
with optional spaces before and after as the input field separator.FNR==NR
is a condition that is true for the first file only (file record number == total record number).a[$3]=$6
stores the 3rd field (fromFile2
) as the key with the 6th field as the value in the arraya
.next
skips further processing and continues with the next line. This ensures that all following code is executed for the second and following files only.$1 in a
is a condition that checks if the 1st field is present as a key ina
.print $0 " | " a[$1]
prints the current input line (fromFile1
) followed by a separator and the value froma
matching the key.
英文:
awk -F' *[|] *' 'FNR==NR { a[$3]=$6; next } $1 in a { print $0 " | " a[$1] }' File2 File1
prints
Username| Status| Managername | Access
4115 | Open | X | Never
Note that File2
is specified first.
Explanation:
-F' *[|] *'
= set|
with optional spaces before and after as input field separatorFNR==NR
= condition that is true for the first file only (file record number == total record number)a[$3]=$6
= store 3rd field (fromFile2
) as key with 6th field as value in arraya
next
= skip further processing, continue with next line. This makes sure all following code is executed for the second and following files only.$1 in a
= condition if the 1st field is present as a key ina
print $0 " | " a[$1]
print the current input line (fromFile1
) followed by a separator and the value froma
matching the key
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论