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.
Taken into consideration your comments, this looks an exercise to replace values that follows
fields:
in txt file, using replacement values from another file where the "fields" value is a kind of key.Have a look in this approach:
Output:
Explanation:
Other solutions with AWK, etc may provide a more efficient code .
A more scalable
Awk
logic can be done something as below.1. replcement-of-field actually involves only the part after the dot
The below command does the job as intended.
produces an output as
OP
needed,Throwing in a bit of explanation,
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,
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.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.A
sed
one-liner:How it works:
Transform file2.csv into
sed
substitute commands. So the initial codesed 's#,projects.#/#;s#.*#/fields/s/&/\;#' file2.csv
outputs:
Run the resulting substitute commands on file1.txt.
Output: