如何将一个文件中的值映射到另一个文件并使用 shell 脚本将其写入另一个文件

问题描述 投票:0回答:1

我有 2 个文本文件。

下面的文本文件是

projectnameid.txt
。第一列是项目名称。项目 ID 中的第二列

basket-items                      9189
cloudfabric-notifications         10789
cloud-ports                       10965
common                            9081
customer-port-management          8550
deploy-quote                      8348
geographical-location-management  8549
internet-connections              9293
ipaddress                         8536
ip-addresses                      9294
order-notifications               11725
order-status                      8353
port-inventory                    8486
port-locations                    8490
pricing-quotes                    8493
product-availability              8488
product-catalogue                 8489
product-countrylist               8492
stub-service                      8510
customer-port-management-sf       10488
internet-connections-order-sf     11166
ip-addresses-order-sf             11165

下面的文本文件是

endfilter3-all-b.txt

337718  10965  "refs/merge-requests/13/head"  "2023-07-19T11:39:41.739Z"
318933  8536   "develop"                      "2023-07-05T11:41:28.482Z"
366210  8549   "develop"                      "2023-08-11T13:49:18.905Z"
338835  8510   "main"                         "2023-07-20T06:45:59.823Z"
135208  8348   "main"                         "2023-02-17T11:25:07.723Z"
115402  8493   "main"                         "2023-02-07T06:52:05.486Z"
361979  9293   "refs/merge-requests/83/head"  "2023-08-09T07:38:32.831Z"
345703  11725  "main"                         "2023-07-26T08:31:11.004Z"
101775  8353   "main"                         "2023-02-02T09:22:47.402Z"
115414  8486   "main"                         "2023-02-07T07:41:35.478Z"
150861  9081   "main"                         "2023-03-13T05:37:31.370Z"
135733  8489   "main"                         "2023-02-17T16:14:51.280Z"

first column is pipeline id, second column is project id, third column is branch name, fourth column is timestamp

我必须先阅读 endfilter3-all-b.txt 然后选择每个项目ID并匹配projectnameid.txt中的项目ID 选择匹配的项目ID和项目名称后将其写入

finalized-project-names.txt

expected output:
(最终项目名称.txt)

cloud-ports                 10965
ipaddress               8536
geographical-location-management    8549
stub-service                8510 
deploy-quote                8348   
pricing-quotes              8493   
internet-connections            9293   
order-notifications         11725  
order-status                8353   
port-inventory              8486   
common                  9081   
product-catalogue           8489    

这是我尝试过的:

#!/bin/bash

> finalized-project-names.txt

project_name_id_file="projectnameid.txt"
while read -r project_name project_id; do
   project_name_id_map[$project_id]=$project_name
done < "$project_name_id_file"

endfilter3_all_b_file="endfilter3-all-b.txt"
while read pipeline_id project_id branch_name timestamp; do
  project_name=${project_name_id_map[$project_id]}
  if [ -n "$project_name" ]; then
    echo "$project_name $project_id" >> finalized-project-names.txt
  fi
done < "$endfilter3_all_b_file"

我得到的输出:

cloud-ports 10965
ipaddress 8536
geographical-location-management 8549
stub-service 8510
deploy-quote 8348
pricing-quotes 8493
internet-connections 9293
order-notifications 11725
order-status 8353
port-inventory 8486
common 9081
product-catalogue 8489
cloud-ports 10965
ipaddress 8536
geographical-location-management 8549
stub-service 8510
deploy-quote 8348
pricing-quotes 8493
internet-connections 9293
order-notifications 11725
order-status 8353
port-inventory 8486
common 9081
product-catalogue 8489

有人可以帮我解决这个问题吗?

linux shell awk sed grep
1个回答
1
投票

使用 Awk 应该非常简单。将项目名称存储在

endfilter3-all-b.txt
文件中的地图数据结构中,并在其他文件
projectnameid.txt
中查找匹配的键并写入输出,

awk 'FNR==NR{key[$2]; next}$2 in key' endfilter3-all-b.txt projectnameid.txt
© www.soinside.com 2019 - 2024. All rights reserved.