gpt4 book ai didi

awk grep 或 sed : how to match two files

转载 作者:行者123 更新时间:2023-12-04 14:04:24 25 4
gpt4 key购买 nike

(你真棒)

如何将参数从之前的命令传递给 awk 或 grep?

我想匹配两个文件中的一个字段(用户名):

预期结果:

firstname   Lastname  Username IPaddress
Mozes Bowman user1 134.244.47.32
Jazzmyn Parrish user2 3.249.198.34
Chet Woods user3 52.215.73.213

来自这 2 个:

**file1**
IPaddress Username
34.244.47.32 user1
3.249.198.34 user2
52.215.73.213 user3

**file2**
firstname Lastname Username
Mozes Bowman user1
Jazzmyn Parrish user2
Chet Woods user3

这是我所能做到的:

awk '{print $2,$1}' file1 | while read Username IP ; do grep $username file2 && echo $IP; done

导致 IP 每次都换行显示:

firstname   Lastname    Username
IPaddress
Mozes Bowman user1
34.244.47.32
Jazzmyn Parrish user2
3.249.198.34
Chet Woods user3
52.215.73.213

最佳答案

你可以试试这个awk

awk 'NR==FNR {r[$3]=$0 ; next } { print r[$2]"\t" $1 }' $file2 $file1

输出

firstname Lastname   Username   IPaddress
Mozes Bowman user1 34.244.47.32
Jazzmyn Parrish user2 3.249.198.34
Chet Woods user3 52.215.73.213

关于awk grep 或 sed : how to match two files,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68987094/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com