将文件中的多个变量值替换为给定的输入值(来自文件)

将文件中的多个变量值替换为给定的输入值(来自文件)

如果我有两个输入文件,其内容如下:如何替换网络名称(例如:P1MSVmgmtvM 替换为 /rest/ethernet-networks/2bf8bc44-5b31-4e6c-99ed-d68b1e9acf9e)。

文件1:(输入文件)

Data_NetworkSet_A
 P1MSVmgmtvM
 P1MSVvMotion
Data_NetworkSet_B
 P2MSVmgmtvM
 P2MSVvMotion
Edge_NetworkSet_A
 E1MSVEDGEiDMZRUE1
 E1MSVEDGEiEXPRUE1

文件 2:(包含 network_name 的值)

    "name": "P2MSVvMotion",
    "uri": "/rest/ethernet-networks/1d3188e4-9c06-4e92-92fd-dde4d6985151",
    "name": "P1MSVmgmtvM",
    "uri": "/rest/ethernet-networks/2bf8bc44-5b31-4e6c-99ed-d68b1e9acf9e",
    "name": "P2MSVmgmtvM",
    "uri": "/rest/ethernet-networks/8be8cdd2-e9a6-4ecd-9aa3-435e810c68e9",
    "name": "E1MSVEDGEiEXPRUE1",
    "uri": "/rest/ethernet-networks/97a47127-5c28-4c5b-891a-5ea3736306d3",
    "name": "P1MSVvMotion",
    "uri": "/rest/ethernet-networks/c16f119a-b556-464d-96dd-7fee9fd8dbc2",
    "name": "E1MSVEDGEiDMZRUE1",
    "uri": "/rest/ethernet-networks/cbb509aa-ab8a-4d85-886b-0899424f324c",

预期输出:

Data_NetworkSet_A
 - /rest/ethernet-networks/2bf8bc44-5b31-4e6c-99ed-d68b1e9acf9e
 - /rest/ethernet-networks/c16f119a-b556-464d-96dd-7fee9fd8dbc2
Data_NetworkSet_B
 - /rest/ethernet-networks/8be8cdd2-e9a6-4ecd-9aa3-435e810c68e9
 - /rest/ethernet-networks/1d3188e4-9c06-4e92-92fd-dde4d6985151
Edge_NetworkSet_A
 - /rest/ethernet-networks/cbb509aa-ab8a-4d85-886b-0899424f324c
 - /rest/ethernet-networks/97a47127-5c28-4c5b-891a-5ea3736306d3

答案1

除了被引用并分成两行的字段之外,这是一个相当标准的查找任务awk

awk -F: '
function strip(x) {
  gsub(/[",]/,"",x)
  return x
}
  NR==FNR {
    i = strip($2)
    if (getline > -1) {
      a[i] = strip($2)
    }
    next
  } 
  $1 in a {
    $1 = " -"a[$1]
  } 1' file2 file1

给予

Data_NetworkSet_A
 - /rest/ethernet-networks/2bf8bc44-5b31-4e6c-99ed-d68b1e9acf9e
 - /rest/ethernet-networks/c16f119a-b556-464d-96dd-7fee9fd8dbc2
Data_NetworkSet_B
 - /rest/ethernet-networks/8be8cdd2-e9a6-4ecd-9aa3-435e810c68e9
 - /rest/ethernet-networks/1d3188e4-9c06-4e92-92fd-dde4d6985151
Edge_NetworkSet_A
 - /rest/ethernet-networks/cbb509aa-ab8a-4d85-886b-0899424f324c
 - /rest/ethernet-networks/97a47127-5c28-4c5b-891a-5ea3736306d3

答案2

  1. 您转换文件 1,以便名称成为变量引用:

    awk '$0 !~ /^ / { print; next; }; { sub("^ *","&$"); print; }' file1         
        Data_NetworkSet_A
         $P1MSVmgmtvM
         $P1MSVvMotion
         ...
    
  2. 您将文件 2 中的数据读入 shell 变量(假设没有PATH出现“危险”名称(如 )):

     while read dummy varname; read dummy varvalue; do
         eval "export ${varname%,}=${varvalue%,}"
     done <file2
    
  3. 您已将envsubst名称替换为值:

     envsubst <file2
         Data_NetworkSet_A
          /rest/ethernet-networks/2bf8bc44-5b31-4e6c-99ed-d68b1e9acf9e
          /rest/ethernet-networks/c16f119a-b556-464d-96dd-7fee9fd8dbc2
    

答案3

另一种解决方案(一体):

IFS=''    #prevent ending/trailing whitespace from being trimmed.
while read line in
do
        if [ ! "$(echo $line | grep -e'^\s')" ] # if the word is not begins from space
        then
                # just write it to result_file
                echo $line >> result_file
        else
                # cut leading space => grep in file_2 (get 1 line after match too) => grep for uri => cut no needed symbols => write it to result_file  
                echo " - $(grep -A1 $(echo "$line" | tr -d ' ') file_2 | grep uri | cut -d ':' -f2 | tr -d '", ')" >> result_file
        fi
done < file_1

该脚本读取行file_1并与 进行比较file_2。结果推了进去result_file

相关内容