Subrat Sahoo Subrat Sahoo - 1 year ago 67
Bash Question

Compare a .txt and .csv file and need to replace with matching name in .csv file to .txt

file1.txt

[fields:WinSpc:defect]
a=b
b=c
hello=hi
[fields:ROCKET PROJECT:ticket]
description=Descrtiption
status=status
[fields:PROJECT_Nexus:defect]
title=summary
priority=Priority_hello


file2.csv

WinSpc,projects.winspc
ROCKET PROJECT,projects.rocket_project
PROJECT_Nexus,projects.project-nexus


I need to match these two files and desired output would be :

output.txt

[fields:winspc:defect]
a=b
b=c
hello=hi
[fields:rocket_project:ticket]
description=Descrtiption
status=status
[fields:project-nexus:defect]
title=summary
priority=Priority_hello


Just the name should be changed,

i have tried using

grep -Fwf, diff --breif,


and awk options but not getting desired output. Still learning these things. Any suggestions would be very helpful. Thanks in advance.

Answer Source

A more scalable Awk logic can be done something as below.

Re-affirming the requirement for future readers, the .csv file has a field,replacement-of-field1 pair stored in multiple lines. For all those field in .csv the corresponding entries in .txt file should be replaced with replacement-of-field


1. replcement-of-field actually involves only the part after the dot

The below command does the job as intended.

awk 'FNR==NR{split($2,list,"."); replacement[$1]=list[2]; next} \
   {for (i in replacement){ if (match($0,i)) {gsub(i,replacement[i],$0); break} }}1 ' \
      FS="," file2.csv file1.txt

produces an output as OP needed,

[fields:winspc:defect]
a=b
b=c
hello=hi
[fields:rocket_project:ticket]
description=Descrtiption
status=status
[fields:project-nexus:defect]
title=summary
priority=Priority_hello

Throwing in a bit of explanation,

  1. FNR==NR logic ensures the command after it within {} is run first for the .csv file. Note that .csv file is read with field-separator ,
  2. split($2,list,".");replacement[$1]=list[2]; next ensures that the second column of the file is split by . and a hash-map is created with index set to value to be replaced and the value as actual value to be replaced. This is done for all the lines in the .csv file
  3. Now on the .txt file, for each line is checked to see if the value to be replaced is present, if present it is replaced with the replacement value.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download