1

我有fileAfileB数据如下图

fileA

,,"user1","email"
,,"user2","email"
,,"user3","email"
,,"user4","email"

fileB

,,user2,location
,,user4,location
,,user1,location
,,user3,location

我想搜索fileA用户fileB并仅获取位置并将该位置添加到 fileA/或其他文件

输出期望像

,,"user1","email",location
,,"user2","email",location
,,"user3","email",location
,,"user4","email",location

我正在尝试逻辑,使用 while 获取fileA用户名并搜索它fileB以获取位置。但是无法将其添加到fileA后面

非常感谢您的帮助

4

2 回答 2

1

你可以完全做到这一点awk

awk -F, '
NR==FNR{a[$3]=$4;next}
{for(x in a) if(index($3,x)>0) print $0","a[x]}' file2 file1

测试:

$ cat file1
,,"user1","email"
,,"user2","email"
,,"user3","email"
,,"user4","email"

$ cat file2
,,user2,location2
,,user4,location4
,,user1,location1
,,user3,location3

$ awk -F, 'NR==FNR{a[$3]=$4;next}{for(x in a) if(index($3,x)>0) print $0","a[x]}' file2 file1
,,"user1","email",location1
,,"user2","email",location2
,,"user3","email",location3
,,"user4","email",location4
于 2013-06-26T04:44:31.940 回答
1

这应该有效:

for user in `awk -F\" '{print $2}' fileA`
do
   loc=`grep ${user} fileB | awk -F',' '{print $4}'`
   sed -i "/${user}/ s/$/,${loc}/" fileA
done

添加示例:

$ cat fileA
,,"user1","email"
,,"user2","email"
,,"user3","email"
,,"user4","email"

$ cat fileB
,,user2,location2
,,user4,location4
,,user1,location1
,,user3,location3

$ for user in `awk -F\" '{print $2}' fileA`; do echo ${user}; loc=`grep ${user} fileB |    awk -F',' '{print $4}'`; echo ${loc}; sed -i "/${user}/ s/$/,${loc}/" fileA; done

$ cat fileA
,,"user1","email",location1
,,"user2","email",location2
,,"user3","email",location3
,,"user4","email",location4

描述不清楚,但根据问题,您可以使用以下命令将值/数据附加到 Unix 中每一行的末尾

sed -i '/search_pattern/ s/$/string_to_be_appended/' filename
于 2013-06-25T18:00:22.597 回答