如何使用shell脚本将值从一个文件Map到另一个文件并将其写入另一个文件

disho6za  于 12个月前  发布在  Shell
关注(0)|答案(2)|浏览(131)

我有两个文本文件。
下面的文本文件是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,并在选择匹配的项目ID和项目名称后将其写入finalized-project-names.txt
expected output:(finalized-project-names.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

有人能帮我弄清楚吗?

mmvthczy

mmvthczy1#

应该是非常直接的与Awk。将项目名称存储在endfilter3-all-b.txt文件中的Map数据结构中,并在另一个文件projectnameid.txt中查找匹配的键,并写入输出,

awk 'FNR==NR{key[$2]; next}$2 in key' endfilter3-all-b.txt projectnameid.txt

如果上面的结果看起来不错,请使用重定向操作符将输出写入目标文件。将> finalized-project-names.txt添加到上述命令的末尾。

wgx48brx

wgx48brx2#

当你添加grep标签时,这里有一个grep解决方案:

grep -E -f <(sed -E "s/^[0-9]+[[:blank:]]+([0-9]+).*/[[:blank:]]\1$/" endfilter3-all-b.txt) projectnameid.txt

输出量:

cloud-ports                       10965
common                            9081
deploy-quote                      8348
geographical-location-management  8549
internet-connections              9293
ipaddress                         8536
order-notifications               11725
order-status                      8353
port-inventory                    8486
pricing-quotes                    8493
product-catalogue                 8489
stub-service                      8510
  • sed命令s/^[0-9]+[[:blank:]]+([0-9]+).*/[[:blank:]]\1$/提取endfilter3-all-b.txt的第二个字段,然后将模式[[:blank:]]前置到它,并使用$进行锚以避免部分匹配。
  • 在bash process substitution(由<(commnd)表示)的帮助下,sed命令的输出被视为馈送到grep -f的文件。

相关问题