我是一个用bash学习shell脚本的初学者。
我有两个不同的文件:Professionals.txt
ProID:ProName:CSLocal:n1:n2
12345:John Joe:CSBerlin:0:0
98765:Miller Key:CSMoscow:0:1
和
People.txt
peopleID:personName:Age:local:phone:n3
10001:Greg Linn:86:Berlin:912345678:0
10002:Peter Ronner:65:London:962345678:0
10003:Kelly Sena:91:Moscow:966645678:0
10004:Anne Tyler:87:Moscow:984973897:0
我需要做一个脚本,所以我得到一个输出文件,如:
output.txt
ProName:ProID:personName:personID:CSLocal
personName
将对应与Professional在同一城市的人
Miller Key:98765:Kelly Sena:10003:CSMoscow
Miller Key:98765:Anne Tyler:10004:CSMoscow
致意。
join -t: -1 3 -2 4 -o1.2,1.1,2.2,2.1,2.4
<(sort -t: -k3,3 Professionals.txt )
<(sort -t: -k4,4 People.txt | sed 's/^(([^:]*:){3})/1CS/')
- 与完全满足了您的需求:它根据给定的列匹配两个列表。但是它需要将列表按列排序,所以这就是其余代码所做的。
-t
用于sort和与指定列分隔符-1
和-2
telljoin在各自的列表中加入哪些列-k
告诉sort在哪个列上排序,3,3
表示"只使用第3列">-o
告诉加入输出哪些列- sed用于为People.txt列表中的城市添加
CS
前缀,以便在两个列表 中匹配名称
使用GNU awk:
awk -F: 'FNR==NR { map[$4][$2]=$2":"$1;next } { for ( i in map[substr($3,3)] ) { print $2":"$1":"map[substr($3,3)][i]":"$3 } }' People.txt Professionals.txt
解释:
awk -F: 'FNR==NR { # Process the first file (People.txt)
map[$4][$2]=$2":"$1; # Build a two dimensional array with the city as the first index and the name as the second. Have the name and the Id as the value
next
}
{ # Process the second file
for ( i in map[substr($3,3)] ) {
print $2":"$1":"map[substr($3,3)][i]":"$3 # Loop through the array where the first index is equal to the 3rd ":" separated field from the 3rd character onwards of the existing line, printing the data along with additional data from the existing line.
}
}' People.txt Professionals.txt